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::setDefaultPath(string pathname) {
\r
46 //add / to name if needed
\r
47 string lastChar = pathname.substr(pathname.length()-1);
\r
48 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
\r
49 if (lastChar != "/") { pathname += "/"; }
\r
51 if (lastChar != "\\") { pathname += "\\"; }
\r
54 defaultPath = pathname;
\r
57 catch(exception& e) {
\r
58 errorOut(e, "MothurOut", "setDefaultPath");
\r
62 /*********************************************************************************************/
\r
63 void MothurOut::closeLog() {
\r
68 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
\r
70 if (pid == 0) { //only one process should output to screen
\r
79 catch(exception& e) {
\r
80 errorOut(e, "MothurOut", "closeLog");
\r
85 /*********************************************************************************************/
\r
86 MothurOut::~MothurOut() {
\r
88 _uniqueInstance = 0;
\r
91 catch(exception& e) {
\r
92 errorOut(e, "MothurOut", "MothurOut");
\r
96 /*********************************************************************************************/
\r
97 void MothurOut::mothurOut(string output) {
\r
102 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
\r
104 if (pid == 0) { //only one process should output to screen
\r
114 catch(exception& e) {
\r
115 errorOut(e, "MothurOut", "MothurOut");
\r
119 /*********************************************************************************************/
\r
120 void MothurOut::mothurOutEndLine() {
\r
124 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
\r
126 if (pid == 0) { //only one process should output to screen
\r
136 catch(exception& e) {
\r
137 errorOut(e, "MothurOut", "MothurOutEndLine");
\r
141 /*********************************************************************************************/
\r
142 void MothurOut::mothurOutJustToLog(string output) {
\r
146 MPI_Comm_rank(MPI_COMM_WORLD, &pid);
\r
148 if (pid == 0) { //only one process should output to screen
\r
157 catch(exception& e) {
\r
158 errorOut(e, "MothurOut", "MothurOutJustToLog");
\r
162 /*********************************************************************************************/
\r
163 void MothurOut::errorOut(exception& e, string object, string function) {
\r
165 //mem_usage(vm, rss);
\r
167 mothurOut("Error: ");
\r
168 mothurOut(toString(e.what()));
\r
169 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
170 mothurOutEndLine();
\r
172 /*********************************************************************************************/
\r
173 //The following was originally from http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c
\r
174 // process_mem_usage(double &, double &) - takes two doubles by reference,
\r
175 // attempts to read the system-dependent data for a process' virtual memory
\r
176 // size and resident set size, and return the results in KB.
\r
178 // On failure, returns 0.0, 0.0
\r
179 int MothurOut::mem_usage(double& vm_usage, double& resident_set) {
\r
180 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
\r
183 resident_set = 0.0;
\r
185 // 'file' stat seems to give the most reliable results
\r
187 ifstream stat_stream("/proc/self/stat",ios_base::in);
\r
189 // dummy vars for leading entries in stat that we don't care about
\r
191 string pid, comm, state, ppid, pgrp, session, tty_nr;
\r
192 string tpgid, flags, minflt, cminflt, majflt, cmajflt;
\r
193 string utime, stime, cutime, cstime, priority, nice;
\r
194 string O, itrealvalue, starttime;
\r
196 // the two fields we want
\r
198 unsigned long vsize;
\r
201 stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
\r
202 >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
\r
203 >> utime >> stime >> cutime >> cstime >> priority >> nice
\r
204 >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
\r
206 long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
\r
207 vm_usage = vsize / 1024.0;
\r
208 resident_set = rss * page_size_kb;
\r
210 mothurOut("Memory Usage: vm = " + toString(vm_usage) + " rss = " + toString(resident_set) + "\n");
\r
214 /* //windows memory usage
\r
215 // Get the list of process identifiers.
\r
216 DWORD aProcesses[1024], cbNeeded, cProcesses;
\r
218 if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ){ return 1; }
\r
220 // Calculate how many process identifiers were returned.
\r
221 cProcesses = cbNeeded / sizeof(DWORD);
\r
223 // Print the memory usage for each process
\r
224 for (int i = 0; i < cProcesses; i++ ) {
\r
225 DWORD processID = aProcesses[i];
\r
227 PROCESS_MEMORY_COUNTERS pmc;
\r
229 HANDLE hProcess = OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_VM_READ), FALSE, processID);
\r
231 // Print the process identifier.
\r
232 printf( "\nProcess ID: %u\n", processID);
\r
234 if (NULL != hProcess) {
\r
236 if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) {
\r
237 printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
\r
238 printf( "\tPeakWorkingSetSize: 0x%08X\n", pmc.PeakWorkingSetSize );
\r
239 printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
\r
240 printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakPagedPoolUsage );
\r
241 printf( "\tQuotaPagedPoolUsage: 0x%08X\n", pmc.QuotaPagedPoolUsage );
\r
242 printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", pmc.QuotaPeakNonPagedPoolUsage );
\r
243 printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", pmc.QuotaNonPagedPoolUsage );
\r
244 printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
\r
245 printf( "\tPeakPagefileUsage: 0x%08X\n", pmc.PeakPagefileUsage );
\r
247 CloseHandle(hProcess);
\r
255 /*********************************************************************************************/
\r