Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef SRC_PROCFS_BYTE_COUNTER_H_ | |
| 6 #define SRC_PROCFS_BYTE_COUNTER_H_ | |
| 7 | |
| 8 #include <stdio.h> | |
| 9 | |
| 10 #include <glib.h> | |
| 11 #include <sys/socket.h> | |
| 12 #include <linux/netdevice.h> | |
| 13 | |
| 14 #include <string> | |
| 15 | |
| 16 #include <base/basictypes.h> // NOLINT | |
| 17 | |
| 18 #include "src/byte_counter.h" | |
| 19 | |
| 20 namespace cashew { | |
| 21 | |
| 22 // an procfs-based implementation of the ByteCounter interface | |
|
Jason Glasgow
2010/11/19 21:51:08
a procfs
Vince Laviano
2010/11/20 00:32:02
Done.
| |
| 23 class ProcfsByteCounter: public ByteCounter { | |
| 24 public: | |
| 25 explicit ProcfsByteCounter(const std::string& interface); | |
| 26 virtual ~ProcfsByteCounter(); | |
| 27 virtual const std::string& GetInterface() const; | |
| 28 virtual uint64 GetRxBytes() const; | |
| 29 virtual uint64 GetTxBytes() const; | |
| 30 virtual void SetDelegate(ByteCounterDelegate *delegate); | |
| 31 | |
| 32 private: | |
| 33 // interface name | |
| 34 const std::string interface_; | |
| 35 | |
| 36 // file pointer for open /proc/net/dev virtual file | |
| 37 // can be NULL | |
| 38 FILE *proc_net_dev_fp_; | |
| 39 | |
| 40 // ReadStats timer glib source id | |
| 41 guint read_stats_source_id_; | |
| 42 | |
| 43 // counter: received bytes | |
| 44 uint64 counter_rx_bytes_; | |
| 45 | |
| 46 // counter: transmitted byes | |
| 47 uint64 counter_tx_bytes_; | |
| 48 | |
| 49 // baseline stats against which we'll compare our next update | |
| 50 // updated with each sample so as to better handle wraparound | |
| 51 // | |
| 52 // these stats are maintained by the device driver starting from kernel boot | |
| 53 // or module load time and have aribitrary values relative to when our | |
| 54 // logical |counter_rx_bytes_| and |counter_tx_bytes_| counters were started | |
| 55 struct net_device_stats baseline_stats_; | |
| 56 | |
| 57 // have our baseline device stats been initialized? | |
| 58 // this becomes true after the first call to OnStatsUpdate | |
| 59 bool baseline_initialized_; | |
| 60 | |
| 61 // delegate | |
| 62 ByteCounterDelegate *delegate_; | |
| 63 | |
| 64 // open /proc/net/dev | |
| 65 // stores resulting fp in |proc_net_dev_fp_| | |
| 66 // returns true on success and false on failure | |
| 67 bool OpenProcNetDev(); | |
| 68 | |
| 69 // close /proc/net/dev | |
| 70 // clears |proc_net_dev_fp_| | |
| 71 // ok to call even if file isn't open | |
| 72 void CloseProcNetDev(); | |
| 73 | |
| 74 // glib integration: static wrapper for ReadStats | |
| 75 // takes object ptr as data and invokes object->ReadStats | |
| 76 static gboolean StaticReadStatsCallback(gpointer data); | |
| 77 | |
| 78 // read stats for |interface_| from /proc/net/dev | |
| 79 // returns true on success and false on failure | |
| 80 bool ReadStats(); | |
| 81 | |
| 82 // new stats are available, update counter | |
| 83 void OnStatsUpdate(const struct net_device_stats& stats); | |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(ProcfsByteCounter); | |
| 86 }; | |
| 87 | |
| 88 } // namespace cashew | |
| 89 | |
| 90 #endif // SRC_PROCFS_BYTE_COUNTER_H_ | |
| OLD | NEW |