Chromium Code Reviews| Index: src/procfs_byte_counter.h |
| diff --git a/src/procfs_byte_counter.h b/src/procfs_byte_counter.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f11b45fabeb58636591ca4e52419b0b658ae6ac4 |
| --- /dev/null |
| +++ b/src/procfs_byte_counter.h |
| @@ -0,0 +1,90 @@ |
| +// Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef SRC_PROCFS_BYTE_COUNTER_H_ |
| +#define SRC_PROCFS_BYTE_COUNTER_H_ |
| + |
| +#include <stdio.h> |
| + |
| +#include <glib.h> |
| +#include <sys/socket.h> |
| +#include <linux/netdevice.h> |
| + |
| +#include <string> |
| + |
| +#include <base/basictypes.h> // NOLINT |
| + |
| +#include "src/byte_counter.h" |
| + |
| +namespace cashew { |
| + |
| +// 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.
|
| +class ProcfsByteCounter: public ByteCounter { |
| + public: |
| + explicit ProcfsByteCounter(const std::string& interface); |
| + virtual ~ProcfsByteCounter(); |
| + virtual const std::string& GetInterface() const; |
| + virtual uint64 GetRxBytes() const; |
| + virtual uint64 GetTxBytes() const; |
| + virtual void SetDelegate(ByteCounterDelegate *delegate); |
| + |
| + private: |
| + // interface name |
| + const std::string interface_; |
| + |
| + // file pointer for open /proc/net/dev virtual file |
| + // can be NULL |
| + FILE *proc_net_dev_fp_; |
| + |
| + // ReadStats timer glib source id |
| + guint read_stats_source_id_; |
| + |
| + // counter: received bytes |
| + uint64 counter_rx_bytes_; |
| + |
| + // counter: transmitted byes |
| + uint64 counter_tx_bytes_; |
| + |
| + // baseline stats against which we'll compare our next update |
| + // updated with each sample so as to better handle wraparound |
| + // |
| + // these stats are maintained by the device driver starting from kernel boot |
| + // or module load time and have aribitrary values relative to when our |
| + // logical |counter_rx_bytes_| and |counter_tx_bytes_| counters were started |
| + struct net_device_stats baseline_stats_; |
| + |
| + // have our baseline device stats been initialized? |
| + // this becomes true after the first call to OnStatsUpdate |
| + bool baseline_initialized_; |
| + |
| + // delegate |
| + ByteCounterDelegate *delegate_; |
| + |
| + // open /proc/net/dev |
| + // stores resulting fp in |proc_net_dev_fp_| |
| + // returns true on success and false on failure |
| + bool OpenProcNetDev(); |
| + |
| + // close /proc/net/dev |
| + // clears |proc_net_dev_fp_| |
| + // ok to call even if file isn't open |
| + void CloseProcNetDev(); |
| + |
| + // glib integration: static wrapper for ReadStats |
| + // takes object ptr as data and invokes object->ReadStats |
| + static gboolean StaticReadStatsCallback(gpointer data); |
| + |
| + // read stats for |interface_| from /proc/net/dev |
| + // returns true on success and false on failure |
| + bool ReadStats(); |
| + |
| + // new stats are available, update counter |
| + void OnStatsUpdate(const struct net_device_stats& stats); |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ProcfsByteCounter); |
| +}; |
| + |
| +} // namespace cashew |
| + |
| +#endif // SRC_PROCFS_BYTE_COUNTER_H_ |