gator: Version 5.18
[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         }
38         mxmlElementSetAttrf(captured, "protocol", "%d", PROTOCOL_VERSION);
39         if (includeTime) { // Send the following only after the capture is complete
40                 if (time(NULL) > 1267000000) { // If the time is reasonable (after Feb 23, 2010)
41                         mxmlElementSetAttrf(captured, "created", "%lu", time(NULL)); // Valid until the year 2038
42                 }
43         }
44
45         target = mxmlNewElement(captured, "target");
46         mxmlElementSetAttr(target, "name", gSessionData->mCoreName);
47         mxmlElementSetAttrf(target, "sample_rate", "%d", gSessionData->mSampleRate);
48         mxmlElementSetAttrf(target, "cores", "%d", gSessionData->mCores);
49         mxmlElementSetAttrf(target, "cpuid", "0x%x", gSessionData->mMaxCpuId);
50
51         if (!gSessionData->mOneShot && (gSessionData->mSampleRate > 0)) {
52                 mxmlElementSetAttr(target, "supports_live", "yes");
53         }
54
55         if (gSessionData->mLocalCapture) {
56                 mxmlElementSetAttr(target, "local_capture", "yes");
57         }
58
59         mxml_node_t *counters = NULL;
60         for (x = 0; x < MAX_PERFORMANCE_COUNTERS; x++) {
61                 const Counter & counter = gSessionData->mCounters[x];
62                 if (counter.isEnabled()) {
63                         if (counters == NULL) {
64                                 counters = mxmlNewElement(captured, "counters");
65                         }
66                         mxml_node_t *const node = mxmlNewElement(counters, "counter");
67                         mxmlElementSetAttrf(node, "key", "0x%x", counter.getKey());
68                         mxmlElementSetAttr(node, "type", counter.getType());
69                         mxmlElementSetAttrf(node, "event", "0x%x", counter.getEvent());
70                         if (counter.getCount() > 0) {
71                                 mxmlElementSetAttrf(node, "count", "%d", counter.getCount());
72                         }
73                 }
74         }
75
76         return xml;
77 }
78
79 char* CapturedXML::getXML(bool includeTime) {
80         char* xml_string;
81         mxml_node_t *xml = getTree(includeTime);
82         xml_string = mxmlSaveAllocString(xml, mxmlWhitespaceCB);
83         mxmlDelete(xml);
84         return xml_string;
85 }
86
87 void CapturedXML::write(char* path) {
88         char file[PATH_MAX];
89
90         // Set full path
91         snprintf(file, PATH_MAX, "%s/captured.xml", path);
92         
93         char* xml = getXML(true);
94         if (util->writeToDisk(file, xml) < 0) {
95                 logg->logError(__FILE__, __LINE__, "Error writing %s\nPlease verify the path.", file);
96                 handleException();
97         }
98
99         free(xml);
100 }
101
102 // whitespace callback utility function used with mini-xml
103 const char * mxmlWhitespaceCB(mxml_node_t *node, int loc) {
104         const char *name;
105
106         name = mxmlGetElement(node);
107
108         if (loc == MXML_WS_BEFORE_OPEN) {
109                 // Single indentation
110                 if (!strcmp(name, "target") || !strcmp(name, "counters"))
111                         return("\n  ");
112
113                 // Double indentation
114                 if (!strcmp(name, "counter"))
115                         return("\n    ");
116
117                 // Avoid a carriage return on the first line of the xml file
118                 if (!strncmp(name, "?xml", 4))
119                         return(NULL);
120
121                 // Default - no indentation
122                 return("\n");
123         }
124
125         if (loc == MXML_WS_BEFORE_CLOSE) {
126                 // No indentation
127                 if (!strcmp(name, "captured"))
128                         return("\n");
129
130                 // Single indentation
131                 if (!strcmp(name, "counters"))
132                         return("\n  ");
133
134                 // Default - no carriage return
135                 return(NULL);
136         }
137
138         return(NULL);
139 }