Merge tag 'v3.10.63' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / CapturedXML.cpp
1 /**
2  * Copyright (C) ARM Limited 2010-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 "CapturedXML.h"
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include <dirent.h>
14
15 #include "SessionData.h"
16 #include "Logging.h"
17 #include "OlyUtility.h"
18
19 CapturedXML::CapturedXML() {
20 }
21
22 CapturedXML::~CapturedXML() {
23 }
24
25 mxml_node_t* CapturedXML::getTree(bool includeTime) {
26         mxml_node_t *xml;
27         mxml_node_t *captured;
28         mxml_node_t *target;
29         int x;
30
31         xml = mxmlNewXML("1.0");
32
33         captured = mxmlNewElement(xml, "captured");
34         mxmlElementSetAttr(captured, "version", "1");
35         if (gSessionData->perf.isSetup()) {
36                 mxmlElementSetAttr(captured, "type", "Perf");
37                 mxmlElementSetAttr(captured, "perf_beta", "yes");
38         }
39         mxmlElementSetAttrf(captured, "protocol", "%d", PROTOCOL_VERSION);
40         if (includeTime) { // Send the following only after the capture is complete
41                 if (time(NULL) > 1267000000) { // If the time is reasonable (after Feb 23, 2010)
42                         mxmlElementSetAttrf(captured, "created", "%lu", time(NULL)); // Valid until the year 2038
43                 }
44         }
45
46         target = mxmlNewElement(captured, "target");
47         mxmlElementSetAttr(target, "name", gSessionData->mCoreName);
48         mxmlElementSetAttrf(target, "sample_rate", "%d", gSessionData->mSampleRate);
49         mxmlElementSetAttrf(target, "cores", "%d", gSessionData->mCores);
50         mxmlElementSetAttrf(target, "cpuid", "0x%x", gSessionData->mMaxCpuId);
51
52         if (!gSessionData->mOneShot && (gSessionData->mSampleRate > 0)) {
53                 mxmlElementSetAttr(target, "supports_live", "yes");
54         }
55
56         if (gSessionData->mLocalCapture) {
57                 mxmlElementSetAttr(target, "local_capture", "yes");
58         }
59
60         mxml_node_t *counters = NULL;
61         for (x = 0; x < MAX_PERFORMANCE_COUNTERS; x++) {
62                 const Counter & counter = gSessionData->mCounters[x];
63                 if (counter.isEnabled()) {
64                         if (counters == NULL) {
65                                 counters = mxmlNewElement(captured, "counters");
66                         }
67                         mxml_node_t *const node = mxmlNewElement(counters, "counter");
68                         mxmlElementSetAttrf(node, "key", "0x%x", counter.getKey());
69                         mxmlElementSetAttr(node, "type", counter.getType());
70                         if (counter.getEvent() != -1) {
71                                 mxmlElementSetAttrf(node, "event", "0x%x", counter.getEvent());
72                         }
73                         if (counter.getCount() > 0) {
74                                 mxmlElementSetAttrf(node, "count", "%d", counter.getCount());
75                         }
76                         if (counter.getCores() > 0) {
77                                 mxmlElementSetAttrf(node, "cores", "%d", counter.getCores());
78                         }
79                 }
80         }
81
82         return xml;
83 }
84
85 char* CapturedXML::getXML(bool includeTime) {
86         char* xml_string;
87         mxml_node_t *xml = getTree(includeTime);
88         xml_string = mxmlSaveAllocString(xml, mxmlWhitespaceCB);
89         mxmlDelete(xml);
90         return xml_string;
91 }
92
93 void CapturedXML::write(char* path) {
94         char file[PATH_MAX];
95
96         // Set full path
97         snprintf(file, PATH_MAX, "%s/captured.xml", path);
98
99         char* xml = getXML(true);
100         if (util->writeToDisk(file, xml) < 0) {
101                 logg->logError(__FILE__, __LINE__, "Error writing %s\nPlease verify the path.", file);
102                 handleException();
103         }
104
105         free(xml);
106 }
107
108 // whitespace callback utility function used with mini-xml
109 const char * mxmlWhitespaceCB(mxml_node_t *node, int loc) {
110         const char *name;
111
112         name = mxmlGetElement(node);
113
114         if (loc == MXML_WS_BEFORE_OPEN) {
115                 // Single indentation
116                 if (!strcmp(name, "target") || !strcmp(name, "counters"))
117                         return "\n  ";
118
119                 // Double indentation
120                 if (!strcmp(name, "counter"))
121                         return "\n    ";
122
123                 // Avoid a carriage return on the first line of the xml file
124                 if (!strncmp(name, "?xml", 4))
125                         return NULL;
126
127                 // Default - no indentation
128                 return "\n";
129         }
130
131         if (loc == MXML_WS_BEFORE_CLOSE) {
132                 // No indentation
133                 if (!strcmp(name, "captured"))
134                         return "\n";
135
136                 // Single indentation
137                 if (!strcmp(name, "counters"))
138                         return "\n  ";
139
140                 // Default - no carriage return
141                 return NULL;
142         }
143
144         return NULL;
145 }