2 * Copyright (C) ARM Limited 2013-2015. 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 #include "MemInfoDriver.h"
12 #include "SessionData.h"
14 class MemInfoCounter : public DriverCounter {
16 MemInfoCounter(DriverCounter *next, char *const name, int64_t *const value);
22 int64_t *const mValue;
24 // Intentionally unimplemented
25 MemInfoCounter(const MemInfoCounter &);
26 MemInfoCounter &operator=(const MemInfoCounter &);
29 MemInfoCounter::MemInfoCounter(DriverCounter *next, char *const name, int64_t *const value) : DriverCounter(next, name), mValue(value) {
32 MemInfoCounter::~MemInfoCounter() {
35 int64_t MemInfoCounter::read() {
39 MemInfoDriver::MemInfoDriver() : mBuf(), mMemUsed(0), mMemFree(0), mBuffers(0) {
42 MemInfoDriver::~MemInfoDriver() {
45 void MemInfoDriver::readEvents(mxml_node_t *const) {
46 // Only for use with perf
47 if (!gSessionData->perf.isSetup()) {
51 setCounters(new MemInfoCounter(getCounters(), strdup("Linux_meminfo_memused2"), &mMemUsed));
52 setCounters(new MemInfoCounter(getCounters(), strdup("Linux_meminfo_memfree"), &mMemFree));
53 setCounters(new MemInfoCounter(getCounters(), strdup("Linux_meminfo_bufferram"), &mBuffers));
56 void MemInfoDriver::read(Buffer *const buffer) {
57 if (!countersEnabled()) {
61 if (!mBuf.read("/proc/meminfo")) {
62 logg->logError("Failed to read /proc/meminfo");
66 char *key = mBuf.getBuf();
69 while ((colon = strchr(key, ':')) != NULL) {
70 char *end = strchr(colon + 1, '\n');
76 if (strcmp(key, "MemTotal") == 0) {
77 memTotal = strtoll(colon + 1, NULL, 10) << 10;
78 } else if (strcmp(key, "MemFree") == 0) {
79 mMemFree = strtoll(colon + 1, NULL, 10) << 10;
80 } else if (strcmp(key, "Buffers") == 0) {
81 mBuffers = strtoll(colon + 1, NULL, 10) << 10;
90 mMemUsed = memTotal - mMemFree;