]> git.donarmstrong.com Git - mothur.git/blobdiff - mothurout.cpp
working on sffinfo command.
[mothur.git] / mothurout.cpp
index ca1a40b32ddbd86e185ddee6e59366e4b3aace94..5e0f3d8aaa85842693ef0ad709a1f761cd33ca7d 100644 (file)
@@ -141,12 +141,99 @@ void MothurOut::mothurOutJustToLog(string output) {
 }\r
 /*********************************************************************************************/\r
 void MothurOut::errorOut(exception& e, string object, string function) {\r
+       double vm, rss;\r
+       //mem_usage(vm, rss);\r
+       \r
        mothurOut("Error: ");\r
        mothurOut(toString(e.what()));\r
        mothurOut(" has occurred in the " + object + " class function " + function + ". Please contact Pat Schloss at mothur.bugs@gmail.com, and be sure to include the mothur.logFile with your inquiry.");\r
        mothurOutEndLine();\r
 }\r
 /*********************************************************************************************/\r
+//The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c \r
+// process_mem_usage(double &, double &) - takes two doubles by reference,\r
+// attempts to read the system-dependent data for a process' virtual memory\r
+// size and resident set size, and return the results in KB.\r
+//\r
+// On failure, returns 0.0, 0.0\r
+int MothurOut::mem_usage(double& vm_usage, double& resident_set) {\r
+  #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)\r
+  \r
+          vm_usage     = 0.0;\r
+          resident_set = 0.0;\r
+\r
+          // 'file' stat seems to give the most reliable results\r
+          //\r
+          ifstream stat_stream("/proc/self/stat",ios_base::in);\r
+\r
+          // dummy vars for leading entries in stat that we don't care about\r
+          //\r
+          string pid, comm, state, ppid, pgrp, session, tty_nr;\r
+          string tpgid, flags, minflt, cminflt, majflt, cmajflt;\r
+          string utime, stime, cutime, cstime, priority, nice;\r
+          string O, itrealvalue, starttime;\r
+\r
+          // the two fields we want\r
+          //\r
+          unsigned long vsize;\r
+          long rss;\r
+\r
+          stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr\r
+                                  >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt\r
+                                  >> utime >> stime >> cutime >> cstime >> priority >> nice\r
+                                  >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest\r
+\r
+          long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages\r
+          vm_usage     = vsize / 1024.0;\r
+          resident_set = rss * page_size_kb;\r
+          \r
+          mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");\r
+               return 0;\r
+\r
+       #else\r
+/*             //windows memory usage\r
+               // Get the list of process identifiers.\r
+               DWORD aProcesses[1024], cbNeeded, cProcesses;\r
+               \r
+               if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }\r
+\r
+               // Calculate how many process identifiers were returned.\r
+               cProcesses = cbNeeded / sizeof(DWORD);\r
+\r
+               // Print the memory usage for each process\r
+               for (int i = 0; i < cProcesses; i++ ) {\r
+                       DWORD processID = aProcesses[i];\r
+                       \r
+                       PROCESS_MEMORY_COUNTERS pmc;\r
+\r
+                       HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);\r
+\r
+                       // Print the process identifier.\r
+                       printf( "\nProcess ID: %u\n", processID);\r
+                       \r
+                       if (NULL != hProcess) {\r
+\r
+                               if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {\r
+                                       printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );\r
+                                       printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );\r
+                                       printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );\r
+                                       printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );\r
+                                       printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );\r
+                                       printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );\r
+                                       printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );\r
+                                       printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage ); \r
+                                       printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );\r
+                               }\r
+                               CloseHandle(hProcess);\r
+                       }\r
+               }\r
+*/\r
+                       return 0;\r
+\r
+       #endif\r
+}\r
+/*********************************************************************************************/\r
+\r
 \r
 \r
 \r