00001 /* 00002 * LoadMonitor.h : part of the Mace toolkit for building distributed systems 00003 * 00004 * Copyright (c) 2007, Charles Killian, Dejan Kostic, Ryan Braud, James W. Anderson, John Fisher-Ogden, Calvin Hubble, Duy Nguyen, Justin Burke, David Oppenheimer, Amin Vahdat, Adolfo Rodriguez, Sooraj Bhat 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are met: 00009 * 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the names of Duke University nor The University of 00017 * California, San Diego, nor the names of the authors or contributors 00018 * may be used to endorse or promote products derived from 00019 * this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00022 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00024 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 00025 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00027 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00028 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00029 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 00030 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 * 00032 * ----END-OF-LEGAL-STUFF---- */ 00033 #ifndef __LOAD_MONITOR__H 00034 #define __LOAD_MONITOR__H 00035 // #include <pthread.h> 00036 #include <fstream> 00037 #include <cassert> 00038 #include "Scheduler.h" 00039 00050 00051 class HiResLoadMonitor : public TimerHandler { 00052 public: 00053 class ProcStat { 00054 public: 00055 ProcStat() : utime(0), stime(0), vsize(0), rss(0) { } 00056 uint utime; 00057 uint stime; 00058 uint vsize; 00059 int rss; 00060 }; // ProcStat 00061 00062 void expire(); 00063 static void start(uint64_t freq = DEFAULT_FREQUENCY); 00064 static void stop(); 00065 00066 public: 00067 static const uint64_t DEFAULT_FREQUENCY = 1*1000*1000; 00068 00069 private: 00070 HiResLoadMonitor(uint64_t freq); 00071 void readProcStat(ProcStat& stat); 00072 00073 private: 00074 static HiResLoadMonitor* instance; 00075 bool halt; 00076 uint64_t frequency; 00077 uint64_t prevTime; 00078 pid_t pid; 00079 ProcStat stat; 00080 std::ifstream statfile; 00081 std::string path; 00082 }; // HiResLoadMonitor 00083 00085 class LoadMonitor : public TimerHandler { 00086 public: 00087 LoadMonitor(); 00088 void expire(); 00089 static void runLoadMonitor(); 00090 static void stopLoadMonitor(); 00091 static double getLoad() { return instance ? instance->curLoad[0] : 0; } 00092 00093 public: 00094 static const uint64_t DEFAULT_FREQUENCY = 5*1000*1000; 00095 00096 private: 00097 static LoadMonitor* instance; 00098 bool halt; 00099 uint64_t frequency; 00100 std::ifstream loadfile; 00101 std::ifstream cpufile; 00102 double load; 00103 double curLoad[3]; 00104 uint32_t lastCPU[4]; 00105 uint32_t curCPU[4]; 00106 double peakload[3]; 00107 uint32_t lastCPU_sum; 00108 00109 00110 }; 00111 00114 #endif