📄 linebuf.cpp
字号:
// LineBuf.cpp: implementation of the LineBuf class.
//
//////////////////////////////////////////////////////////////////////
//#include "stdafx.h"
#include "LineBuf.h"
#include <iostream>
#include <TCHAR.H>
#include <tlhelp32.h>
#include <time.h>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart))
LineBuf::LineBuf()
{
m_sumflag = false;
m_procid = GetCurrentProcessId();
}
LineBuf::~LineBuf()
{
}
int LineBuf::GetFieldSize()
{
if(m_sumflag){
return GetBufInt(-1);
}else{
if(m_buf.empty())
return 0;
const char* ptr = m_buf.c_str();
for(int i=0; ptr && *(ptr+1) != '\n'; ptr = strchr(ptr+1, ' '), i++)
;
return i;
}
}
string LineBuf::GetBuf(int idx, char sep)
{
if(m_buf.empty())
return "";
const char* ptr = m_buf.c_str();
if(m_sumflag)
idx ++;
for(int i=0; ptr && i<idx; ptr = strchr(ptr+1, sep), i++)
;
if(!ptr)
return 0;
const char* ptrpre = ptr;
if(i>0)
ptrpre ++;
ptr = strchr(ptrpre, sep);
string buf;
if(ptr){
buf.assign(ptrpre, ptr-ptrpre);
}else{
buf = ptrpre;
}
return buf;
}
int LineBuf::GetBufInt(int idx, char sep)
{
return atoi(GetBuf(idx, sep).c_str());
}
void LineBuf::ReadLine(FILE *fp)
{
char szbuf[1024];
fgets(szbuf, 1024, fp);
m_buf = szbuf;
}
void LineBuf::TimeTest()
{
PVOID pProcInfo = NULL;
DWORD dwInfoSize = 0x20000;
PPROCESSINFO pProcessInfo;
long ( __stdcall *NtQuerySystemInformation )( DWORD, PVOID, DWORD, DWORD );
pProcInfo = (PVOID)(new byte[dwInfoSize]);
NtQuerySystemInformation = (long(__stdcall*)(DWORD,PVOID,DWORD,DWORD))
GetProcAddress(GetModuleHandle( "ntdll.dll" ),"NtQuerySystemInformation");
NtQuerySystemInformation(5, pProcInfo,dwInfoSize, 0);
pProcessInfo = (PPROCESSINFO)pProcInfo;
char name[255];
do
{
//string name(pProcessInfo->ProcessName.Buffer, pProcessInfo->ProcessName.Buffer+pProcessInfo->ProcessName.Length/2);
//string name ;
WideCharToMultiByte(CP_ACP,
0,
pProcessInfo->ProcessName.Buffer,
pProcessInfo->ProcessName.Length + 1,
name,
pProcessInfo->ProcessName.Length + 1,
NULL,
NULL);
// cout << _T("Process : ") << name << _T("(") << (DWORD)pProcessInfo->dwProcessID << _T(")") << endl;
cout << "Process : "<< name << (DWORD)pProcessInfo->dwProcessID << endl;
// wcout << _T("\t Create Time : ") << (DWORD)pProcessInfo->CreateTime.QuadPart << endl;
// wcout << _T("\t User Time : ") << (DWORD)pProcessInfo->UserTime.QuadPart << endl;
// wcout << _T("\t Kernel Time : ") << (DWORD)pProcessInfo->KernelTime.QuadPart << endl;
if(pProcessInfo->dwOffset == 0) {
break;
}
pProcessInfo = (PPROCESSINFO)((byte*)pProcessInfo + pProcessInfo->dwOffset);
} while(true);
delete pProcInfo;
}
SysTime LineBuf::GetTime()
{
SysTime mytime;
#if SIM_TIME
time_t t = time(&t);
mytime.m_utime = (float)t;
#else
LARGE_INTEGER litmp;
QueryPerformanceFrequency(&litmp);
double dfFreq = (double)litmp.QuadPart;
PVOID pProcInfo = NULL;
DWORD dwInfoSize = 0x20000;
PPROCESSINFO pProcessInfo;
long ( __stdcall *NtQuerySystemInformation )( DWORD, PVOID, DWORD, DWORD );
pProcInfo = (PVOID)(new byte[dwInfoSize]);
NtQuerySystemInformation = (long(__stdcall*)(DWORD,PVOID,DWORD,DWORD))
GetProcAddress(GetModuleHandle( "ntdll.dll" ),"NtQuerySystemInformation");
NtQuerySystemInformation(5, pProcInfo,dwInfoSize, 0);
pProcessInfo = (PPROCESSINFO)pProcInfo;
char name[255];
do
{
WideCharToMultiByte(CP_ACP,
0,
pProcessInfo->ProcessName.Buffer,
pProcessInfo->ProcessName.Length + 1,
name,
pProcessInfo->ProcessName.Length + 1,
NULL,
NULL);
// cout<<"name = "<<name<<endl;
if(strcmp(name, "fptree.exe") == 0){
//mytime.m_ktime = (float)(pProcessInfo->KernelTime.QuadPart/dfFreq);
//mytime.m_utime = (float)(pProcessInfo->UserTime.QuadPart/dfFreq);
mytime.m_ktime = (double)(pProcessInfo->KernelTime.QuadPart/dfFreq);
mytime.m_utime = (double)(pProcessInfo->UserTime.QuadPart/dfFreq);
mytime.m_vsize = pProcessInfo->dwVirtualBytes/1024;
//mytime.m_vsize = pProcessInfo->dwVirtualBytesPeak/1024;
cout<<"find: ktime="<<mytime.m_ktime<<" utime="<<mytime.m_utime<<endl;
break;
}
if(pProcessInfo->dwOffset == 0) {
break;
}
pProcessInfo = (PPROCESSINFO)((byte*)pProcessInfo + pProcessInfo->dwOffset);
} while(true);
delete pProcInfo;
#endif
return mytime;
}
string LineBuf::GetBuf(const char *key, const char *paramlist, char sep)
{
const char* ptr = strstr(paramlist, key);
ptr = strchr(ptr, '=');
return GetBuf(0, ptr+1, sep);
}
string LineBuf::GetBuf(int idx, const char* buffer, char sep)
{
if(!buffer)
return "";
const char* ptr =buffer;
for(int i=0; ptr && i<idx; ptr = strchr(ptr+1, sep), i++)
;
if(!ptr)
return "";
const char* ptrpre = ptr;
if(i>0)
ptrpre ++;
ptr = strchr(ptrpre, sep);
string buf;
if(ptr){
buf.assign(ptrpre, ptr-ptrpre);
}else{
buf = ptrpre;
}
return buf;
}
SysTime LineBuf::GetTimeEx()
{
FILETIME ctime, etime, ktime, utime;
SYSTEMTIME kstime, ustime;
// PROCESSENTRY32结构对象
PROCESSENTRY32 pe;
//PM_COUNTER PMCounter;
// 创建快照句柄
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
// 先搜索系统中第一个进程的信息
Process32First(hSnapshot, &pe);
// 下面对系统中的所有进程进行枚举,并保存其信息
do{
if(strcmp(pe.szExeFile, "fptree.exe") == 0){
cout<<"Find Process fptree.exe"<<endl;
break;
}
} while (Process32Next(hSnapshot, &pe));
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE,pe.th32ProcessID);
GetProcessTimes(handle, &ctime, &etime, &ktime, &utime);
// GetProcessMemoryInfo(handle, &PMCounter, sizeof(PMCounter));
FileTimeToSystemTime(&ktime, &kstime);
FileTimeToSystemTime(&utime, &ustime);
SysTime tm;
time_t t;
tm.m_ktime = (kstime.wMinute * 60 + kstime.wSecond) * 1000000 + kstime.wMilliseconds;
tm.m_utime = (ustime.wMinute * 60 + ustime.wSecond) * 1000000 + ustime.wMilliseconds;
cout<<"time = "<<time(&t)<<endl;
// 关闭快照句柄
CloseHandle(hSnapshot);
return tm;
}
/*
typedef struct tagPROCESSENTRY32 {
DWORD dwSize; // 结构大小;
DWORD cntUsage; // 此进程的引用计数;
DWORD th32ProcessID; // 进程ID;
DWORD th32DefaultHeapID; // 进程默认堆ID;
DWORD th32ModuleID; // 进程模块ID;
DWORD cntThreads; // 此进程开启的线程计数;
DWORD th32ParentProcessID; // 父进程ID;
LONG pcPriClassBase; // 线程优先权;
DWORD dwFlags; // 保留;
char szExeFile[MAX_PATH]; // 进程全名;
} PROCESSENTRY32;
*/
void LineBuf::TimeTestEx()
{
SysTime tm1 = GetTime(true);
for(int i=0; i<10000000; i++)
;
SysTime tm2 = GetTime(true);
cout<<"sys time: "<<tm2.m_ktime - tm1.m_ktime<<endl;
cout<<"user time: "<<tm2.m_utime - tm1.m_utime<<endl;
}
SysTime LineBuf::GetTime(bool bUnitSecond)
{
SysTime tm;
PVOID pProcInfo = NULL;
DWORD dwInfoSize = 0x20000;
PPROCESSINFO pProcessInfo;
DWORD dwWorkingSet;
bool bResult = false ;
long ( __stdcall *NtQuerySystemInformation )( DWORD, PVOID, DWORD, DWORD );
LARGE_INTEGER lSysTime , lUserTime ;
LONGLONG TotalProcessCPUUsage = 0;
LONGLONG CurrentProcessCPUUsage = 0;
/////////////////////////////////
memset(&lSysTime , 0 ,sizeof(lSysTime)) ;
memset(&lUserTime , 0 ,sizeof(lUserTime)) ;
pProcInfo = (PVOID)(new BYTE[dwInfoSize]);
NtQuerySystemInformation = (long(__stdcall*)(DWORD,PVOID,DWORD,DWORD))
GetProcAddress( GetModuleHandle( "ntdll.dll" ),"NtQuerySystemInformation" );
NtQuerySystemInformation(5,pProcInfo,dwInfoSize,0);
if ( NtQuerySystemInformation == NULL ) return tm ;
pProcessInfo = (PPROCESSINFO)pProcInfo;
do
{
if ( pProcessInfo == NULL ) break ;
if(pProcessInfo->dwProcessID == m_procid)
{
dwWorkingSet = pProcessInfo->dwWorkingSet;
lSysTime.QuadPart += (LONGLONG)pProcessInfo->KernelTime.QuadPart ;
lUserTime.QuadPart += (LONGLONG)pProcessInfo->UserTime.QuadPart ;
tm.m_vsize += pProcessInfo->dwVirtualBytes/1024;
}
if(pProcessInfo->dwOffset == 0){
break;
}
pProcessInfo = (PPROCESSINFO)((BYTE*)pProcessInfo + pProcessInfo->dwOffset);
}
while(true);
delete[] pProcInfo;
tm.m_ktime = (Li2Double(lSysTime)) / 10000.0;
tm.m_utime = (Li2Double(lUserTime)) / 10000.0 ;
if ( bUnitSecond )
{
tm.m_ktime /= 1000 ;
tm.m_utime /= 1000 ;
}
return tm;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -