5 * Created by westcott on 2/25/10.
\r
6 * Copyright 2010 Schloss Lab. All rights reserved.
\r
10 #include "mothurout.h"
\r
12 /******************************************************/
\r
13 MothurOut* MothurOut::getInstance() {
\r
14 if( _uniqueInstance == 0) {
\r
15 _uniqueInstance = new MothurOut();
\r
17 return _uniqueInstance;
\r
19 /*********************************************************************************************/
\r
20 void MothurOut::setFileName(string filename) {
\r
22 logFileName = filename;
\r
26 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
\r
28 if (pid == 0) { //only one process should output to screen
\r
31 openOutputFile(filename, out);
\r
37 catch(exception& e) {
\r
38 errorOut(e, "MothurOut", "setFileName");
\r
42 /*********************************************************************************************/
\r
43 void MothurOut::closeLog() {
\r
48 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
\r
50 if (pid == 0) { //only one process should output to screen
\r
59 catch(exception& e) {
\r
60 errorOut(e, "MothurOut", "closeLog");
\r
65 /*********************************************************************************************/
\r
66 MothurOut::~MothurOut() {
\r
68 _uniqueInstance = 0;
\r
71 catch(exception& e) {
\r
72 errorOut(e, "MothurOut", "MothurOut");
\r
76 /*********************************************************************************************/
\r
77 void MothurOut::mothurOut(string output) {
\r
82 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
\r
84 if (pid == 0) { //only one process should output to screen
\r
94 catch(exception& e) {
\r
95 errorOut(e, "MothurOut", "MothurOut");
\r
99 /*********************************************************************************************/
\r
100 void MothurOut::mothurOutEndLine() {
\r
104 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
\r
106 if (pid == 0) { //only one process should output to screen
\r
116 catch(exception& e) {
\r
117 errorOut(e, "MothurOut", "MothurOutEndLine");
\r
121 /*********************************************************************************************/
\r
122 void MothurOut::mothurOutJustToLog(string output) {
\r
126 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
\r
128 if (pid == 0) { //only one process should output to screen
\r
137 catch(exception& e) {
\r
138 errorOut(e, "MothurOut", "MothurOutJustToLog");
\r
142 /*********************************************************************************************/
\r
143 void MothurOut::errorOut(exception& e, string object, string function) {
\r
145 mem_usage(vm, rss);
\r
147 mothurOut("Error: ");
\r
148 mothurOut(toString(e.what()));
\r
149 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
150 mothurOutEndLine();
\r
152 /*********************************************************************************************/
\r
153 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
\r
154 // process_mem_usage(double &, double &) - takes two doubles by reference,
\r
155 // attempts to read the system-dependent data for a process' virtual memory
\r
156 // size and resident set size, and return the results in KB.
\r
158 // On failure, returns 0.0, 0.0
\r
159 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
\r
160 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
\r
163 resident_set = 0.0;
\r
165 // 'file' stat seems to give the most reliable results
\r
167 ifstream stat_stream("/proc/self/stat",ios_base::in);
\r
169 // dummy vars for leading entries in stat that we don't care about
\r
171 string pid, comm, state, ppid, pgrp, session, tty_nr;
\r
172 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
\r
173 string utime, stime, cutime, cstime, priority, nice;
\r
174 string O, itrealvalue, starttime;
\r
176 // the two fields we want
\r
178 unsigned long vsize;
\r
181 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
\r
182 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
\r
183 >> utime >> stime >> cutime >> cstime >> priority >> nice
\r
184 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
\r
186 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
\r
187 vm_usage = vsize / 1024.0;
\r
188 resident_set = rss * page_size_kb;
\r
190 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
\r
194 /* //windows memory usage
\r
195 // Get the list of process identifiers.
\r
196 DWORD aProcesses[1024], cbNeeded, cProcesses;
\r
198 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
\r
200 // Calculate how many process identifiers were returned.
\r
201 cProcesses = cbNeeded / sizeof(DWORD);
\r
203 // Print the memory usage for each process
\r
204 for (int i = 0; i < cProcesses; i++ ) {
\r
205 DWORD processID = aProcesses[i];
\r
207 PROCESS_MEMORY_COUNTERS pmc;
\r
209 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
\r
211 // Print the process identifier.
\r
212 printf( "\nProcess ID: %u\n", processID);
\r
214 if (NULL != hProcess) {
\r
216 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
\r
217 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
\r
218 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
\r
219 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
\r
220 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
\r
221 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
\r
222 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
\r
223 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
\r
224 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
\r
225 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
\r
227 CloseHandle(hProcess);
\r
235 /*********************************************************************************************/
\r