MastEdm, MAKCim прав в своё время нужна была подобная - вот - держи исходник Примечание - DLOG - это внутрення функция журналирования - можно на printf() поменять.
| Код | //==================================================================== // Global function GetMemoryUsage() - print to log (now to console) info about memory used by this process //==================================================================== #include <fcntl.h> int GetMemoryUsage() { int ret_val = 0; int NumOfParameters = 10; std::string Parameter[] = {"VmPeak","VmSize","VmLck","VmHWM","VmRSS","VmData","VmStk","VmExe","VmLib","VmPTE"}; unsigned long Value[NumOfParameters];
char f_name[32]; sprintf(f_name,"/proc/%u/status",getpid()); int fd = open(f_name, O_RDONLY); if (fd > 0) { lseek(fd, 0L, SEEK_SET); ssize_t num; char l_buf[512]; num = read(fd, l_buf, sizeof(l_buf) - 1); if (num > 0) { l_buf[num]='\0'; char value[32]; const char *head,*tail; for(int i=0;i<NumOfParameters;i++) { head = strstr(l_buf,Parameter[i].c_str()); head = strchr(head,':'); head++; while(*(++head)==' '){}; tail = strchr(head,' '); unsigned long size = (tail - head); strncpy(value,head,size); value[size]='\0'; Value[i] = strtoul(value,NULL,10); } #ifdef MEMORY_DEBUG std::cout << "\t\t Process's INFO:" << std::endl; std::cout << "\t VmPeak: " << Value[0] << std::endl; std::cout << "\t VmSize: " << Value[1] << std::endl; std::cout << "\t VmLck: " << Value[2] << std::endl; std::cout << "\t VmHWM: " << Value[3] << std::endl; std::cout << "\t VmRSS: " << Value[4] << std::endl; std::cout << "\t VmData: " << Value[5] << std::endl; std::cout << "\t VmStk: " << Value[6] << std::endl; std::cout << "\t VmExe: " << Value[7] << std::endl; std::cout << "\t VmLib: " << Value[8] << std::endl; std::cout << "\t VmPTE: " << Value[9] << std::endl; #endif std::cout << "\t\t Process's Memory Usage INFO:" << std::endl; std::cout << "\t All RAM usage for process (inlude: shared library's; data, stack and text segments; resident): " << Value[4]+Value[5]+Value[6]+Value[7]+Value[8] << " kB\n"; std::cout << "\t RAM for resident only: " << Value[4] << " kB [Peek of RAM for resident: " << Value[3] << " kB]\n"; std::cout << "\t Virtual memory usage (swap and on the non-swap partitions): " << Value[1] << " kB [Peek is " << Value[0] << " kB]\n"; std::cout << "\t Size of 'Locked' memory : " << Value[2] << " kB\n"; } else { DLOG("** err : GetProcessInfo() - couldn't read file"); ret_val=2; }
if(close(fd) < 0) { DLOG("** err : GetProcessInfo() - couldn't close file"); ret_val += 4; } } else { DLOG("** err : GetProcessInfo() - couldn't open file"); ret_val=1; } return ret_val; }
|
|