blob: cce15c16fcdc23fe7e31cb2bc3af706a2bd37374 [file] [log] [blame]
Jon Medhurst96b56152014-10-30 18:01:15 +00001/**
2 * Copyright (C) ARM Limited 2013-2014. All rights reserved.
3 *
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.
7 */
8
9#include "MemInfoDriver.h"
10
11#include "Logging.h"
12#include "SessionData.h"
13
14class MemInfoCounter : public DriverCounter {
15public:
16 MemInfoCounter(DriverCounter *next, char *const name, int64_t *const value);
17 ~MemInfoCounter();
18
19 int64_t read();
20
21private:
22 int64_t *const mValue;
23
24 // Intentionally unimplemented
25 MemInfoCounter(const MemInfoCounter &);
26 MemInfoCounter &operator=(const MemInfoCounter &);
27};
28
29MemInfoCounter::MemInfoCounter(DriverCounter *next, char *const name, int64_t *const value) : DriverCounter(next, name), mValue(value) {
30}
31
32MemInfoCounter::~MemInfoCounter() {
33}
34
35int64_t MemInfoCounter::read() {
36 return *mValue;
37}
38
39MemInfoDriver::MemInfoDriver() : mBuf(), mMemUsed(0), mMemFree(0), mBuffers(0) {
40}
41
42MemInfoDriver::~MemInfoDriver() {
43}
44
45void MemInfoDriver::readEvents(mxml_node_t *const) {
46 // Only for use with perf
47 if (!gSessionData->perf.isSetup()) {
48 return;
49 }
50
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));
54}
55
56void MemInfoDriver::read(Buffer *const buffer) {
57 if (!countersEnabled()) {
58 return;
59 }
60
61 if (!mBuf.read("/proc/meminfo")) {
62 logg->logError(__FILE__, __LINE__, "Failed to read /proc/meminfo");
63 handleException();
64 }
65
66 char *key = mBuf.getBuf();
67 char *colon;
68 int64_t memTotal = 0;
69 while ((colon = strchr(key, ':')) != NULL) {
70 char *end = strchr(colon + 1, '\n');
71 if (end != NULL) {
72 *end = '\0';
73 }
74 *colon = '\0';
75
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;
82 }
83
84 if (end == NULL) {
85 break;
86 }
87 key = end + 1;
88 }
89
90 mMemUsed = memTotal - mMemFree;
91
92 super::read(buffer);
93}