2 * Copyright (C) ARM Limited 2013-2014. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 // Define to get format macros from inttypes.h
10 #define __STDC_FORMAT_MACROS
12 #include "NetDriver.h"
17 #include "SessionData.h"
19 class NetCounter : public DriverCounter {
21 NetCounter(DriverCounter *next, char *const name, int64_t *const value);
27 int64_t *const mValue;
30 // Intentionally unimplemented
31 NetCounter(const NetCounter &);
32 NetCounter &operator=(const NetCounter &);
35 NetCounter::NetCounter(DriverCounter *next, char *const name, int64_t *const value) : DriverCounter(next, name), mValue(value), mPrev(0) {
38 NetCounter::~NetCounter() {
41 int64_t NetCounter::read() {
42 int64_t result = *mValue - mPrev;
47 NetDriver::NetDriver() : mBuf(), mReceiveBytes(0), mTransmitBytes(0) {
50 NetDriver::~NetDriver() {
53 void NetDriver::readEvents(mxml_node_t *const) {
54 // Only for use with perf
55 if (!gSessionData->perf.isSetup()) {
59 setCounters(new NetCounter(getCounters(), strdup("Linux_net_rx"), &mReceiveBytes));
60 setCounters(new NetCounter(getCounters(), strdup("Linux_net_tx"), &mTransmitBytes));
63 bool NetDriver::doRead() {
64 if (!countersEnabled()) {
68 if (!mBuf.read("/proc/net/dev")) {
74 if (((key = strchr(mBuf.getBuf(), '\n')) == NULL) ||
75 ((key = strchr(key + 1, '\n')) == NULL)) {
84 while ((colon = strchr(key, ':')) != NULL) {
85 char *end = strchr(colon + 1, '\n');
92 int64_t transmitBytes;
93 const int count = sscanf(colon + 1, " %" SCNu64 " %*u %*u %*u %*u %*u %*u %*u %" SCNu64, &receiveBytes, &transmitBytes);
97 mReceiveBytes += receiveBytes;
98 mTransmitBytes += transmitBytes;
109 void NetDriver::start() {
111 logg->logError(__FILE__, __LINE__, "Unable to read network stats");
114 // Initialize previous values
115 for (DriverCounter *counter = getCounters(); counter != NULL; counter = counter->getNext()) {
116 if (!counter->isEnabled()) {
123 void NetDriver::read(Buffer *const buffer) {
125 logg->logError(__FILE__, __LINE__, "Unable to read network stats");