00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 using namespace std;
00038 #include <inttypes.h>
00039 #include "SwapMonitor.h"
00040 #include "Log.h"
00041 #include "params.h"
00042
00043 SwapMonitor* SwapMonitor::instance = 0;
00044
00045 void SwapMonitor::runSwapMonitor()
00046 {
00047
00048
00049 if( instance == 0 )
00050 {
00051 instance = new SwapMonitor();
00052 }
00053
00054
00055 instance->cancel();
00056 instance->schedule( instance->frequency );
00057 }
00058
00059 void SwapMonitor::stopSwapMonitor()
00060 {
00061 instance->halt = true;
00062 }
00063
00064 inline void SwapMonitor::fillLast()
00065 {
00066
00067
00068 static string header;
00069
00070 bool found_in = false;
00071 while( vmstat_file.good() && found_in == false )
00072 {
00073 vmstat_file >> header >> last_in;
00074 if( header == "pswpin" )
00075 found_in = true;
00076 }
00077
00078 bool found_out = false;
00079 while( vmstat_file.good() && found_out == false )
00080 {
00081 vmstat_file >> header >> last_out;
00082 if( header == "pswpout" )
00083 found_out = true;
00084 }
00085 vmstat_file.seekg( 0, ios::beg );
00086 }
00087
00088 SwapMonitor::SwapMonitor() : halt(false), frequency(DEFAULT_FREQUENCY),
00089 cumulative_in(0), cumulative_out(0),
00090 last_in(0), last_out(0),
00091 peak_in(0), peak_out(0)
00092 {
00093 vmstat_file.open( "/proc/vmstat" );
00094 if( ! vmstat_file.good() )
00095 {
00096 perror( "REPLAY error opening /proc/vmstat" );
00097 return;
00098 }
00099
00100 if( params::containsKey( params::MACE_SWAP_MONITOR_FREQUENCY_MS ) )
00101 {
00102 frequency = (int)( params::get<int>( params::MACE_SWAP_MONITOR_FREQUENCY_MS ) * 1000 );
00103 }
00104
00105 Log::logf( "SwapMonitor", "REPLAY Swap monitoring frequency is %" PRIu64
00106 " usecs.", frequency );
00107
00108
00109 fillLast();
00110 cumulative_in = last_in;
00111 cumulative_out = last_out;
00112 }
00113
00114
00115 void SwapMonitor::expire()
00116 {
00117 fillLast();
00118
00119
00120 double in_ps = ((double) last_in - cumulative_in ) / (frequency/(1000*1000));
00121 double out_ps = ((double) last_out - cumulative_out) / (frequency/(1000*1000));
00122
00123
00124
00125 Log::logf( "SwapMonitor", "(in out peakin peakout) %.2f %.2f %.2f %.2f",
00126 in_ps, out_ps, peak_in, peak_out );
00127
00128 cumulative_in = last_in;
00129 cumulative_out = last_out;
00130
00131
00132 if( peak_in < in_ps )
00133 peak_in = in_ps;
00134 if( peak_out < out_ps )
00135 peak_out = out_ps;
00136
00137 if (!halt)
00138 schedule(frequency);
00139 }