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