1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <unistd.h>
33 #include <sys/wait.h>
34 #include <securec.h>
35
36 #ifdef LOSCFG_FS_VFS
37 #include <fcntl.h>
38 #include <errno.h>
39 #endif
40
41 #include "perf.h"
42 #include "option.h"
43 #include "perf_record.h"
44
45 #define PERF_FILE_MODE 0644
46 static PerfConfigAttr g_recordAttr;
47 static const char *g_savePath = "/storage/data/perf.data";
48
GetEvents(const char * argv)49 static inline int GetEvents(const char *argv)
50 {
51 return ParseEvents(argv, &g_recordAttr.eventsCfg, &g_recordAttr.eventsCfg.eventsNr);
52 }
53
GetTids(const char * argv)54 static inline int GetTids(const char *argv)
55 {
56 return ParseIds(argv, (int *)g_recordAttr.taskIds, &g_recordAttr.taskIdsNr);
57 }
58
GetPids(const char * argv)59 static inline int GetPids(const char *argv)
60 {
61 return ParseIds(argv, (int *)g_recordAttr.processIds, &g_recordAttr.processIdsNr);
62 }
63
64 static PerfOption g_recordOpts[] = {
65 OPTION_CALLBACK("-e", GetEvents),
66 OPTION_CALLBACK("-t", GetTids),
67 OPTION_CALLBACK("-P", GetPids),
68 OPTION_STRING("-o", &g_savePath),
69 OPTION_UINT("-p", &g_recordAttr.eventsCfg.events[0].period),
70 OPTION_UINT("-s", &g_recordAttr.sampleType),
71 OPTION_UINT("-d", &g_recordAttr.eventsCfg.predivided),
72 };
73
PerfRecordAttrInit(void)74 static int PerfRecordAttrInit(void)
75 {
76 PerfConfigAttr attr = {
77 .eventsCfg = {
78 #ifdef LOSCFG_PERF_HW_PMU
79 .type = PERF_EVENT_TYPE_HW,
80 .events = {
81 [0] = {PERF_COUNT_HW_CPU_CYCLES, 0xFFFF},
82 },
83 #elif defined LOSCFG_PERF_TIMED_PMU
84 .type = PERF_EVENT_TYPE_TIMED,
85 .events = {
86 [0] = {PERF_COUNT_CPU_CLOCK, 100},
87 },
88 #elif defined LOSCFG_PERF_SW_PMU
89 .type = PERF_EVENT_TYPE_SW,
90 .events = {
91 [0] = {PERF_COUNT_SW_TASK_SWITCH, 1},
92 },
93 #endif
94 .eventsNr = 1, /* 1 event */
95 .predivided = 0,
96 },
97 .taskIds = {0},
98 .taskIdsNr = 0,
99 .processIds = {0},
100 .processIdsNr = 0,
101 .needSample = 1,
102 .sampleType = PERF_RECORD_IP | PERF_RECORD_CALLCHAIN,
103 };
104
105 return memcpy_s(&g_recordAttr, sizeof(PerfConfigAttr), &attr, sizeof(PerfConfigAttr)) != EOK ? -1 : 0;
106 }
107
PerfWriteFile(const char * filePath,const char * buf,ssize_t bufSize)108 ssize_t PerfWriteFile(const char *filePath, const char *buf, ssize_t bufSize)
109 {
110 #ifdef LOSCFG_FS_VFS
111 int fd = -1;
112 ssize_t totalToWrite = bufSize;
113 ssize_t totalWrite = 0;
114
115 if (filePath == NULL || buf == NULL || bufSize == 0) {
116 return -1;
117 }
118
119 fd = open(filePath, O_CREAT | O_RDWR | O_TRUNC, PERF_FILE_MODE);
120 if (fd < 0) {
121 printf("create file [%s] failed, fd: %d, %s!\n", filePath, fd, strerror(errno));
122 return -1;
123 }
124 while (totalToWrite > 0) {
125 ssize_t writeThisTime = write(fd, buf, totalToWrite);
126 if (writeThisTime < 0) {
127 printf("failed to write file [%s], %s!\n", filePath, strerror(errno));
128 (void)close(fd);
129 return -1;
130 }
131 buf += writeThisTime;
132 totalToWrite -= writeThisTime;
133 totalWrite += writeThisTime;
134 }
135 (void)fsync(fd);
136 (void)close(fd);
137
138 return (totalWrite == bufSize) ? 0 : -1;
139 #else
140 (void)filePath;
141 PerfPrintBuffer(buf, bufSize);
142 return 0;
143 #endif
144 }
145
PerfRecord(int fd,int argc,char ** argv)146 void PerfRecord(int fd, int argc, char **argv)
147 {
148 int ret;
149 int child;
150 char *buf;
151 ssize_t len;
152 SubCmd cmd = {0};
153
154 if (argc < 3) { /* perf record argc is at least 3 */
155 return;
156 }
157
158 ret = PerfRecordAttrInit();
159 if (ret != 0) {
160 printf("perf record attr init failed\n");
161 return;
162 }
163
164 ret = ParseOptions(argc - 2, &argv[2], g_recordOpts, &cmd); /* parse option and cmd begin at index 2 */
165 if (ret != 0) {
166 printf("parse error\n");
167 return;
168 }
169
170 ret = PerfConfig(fd, &g_recordAttr);
171 if (ret != 0) {
172 printf("perf config failed\n");
173 return;
174 }
175
176 PerfStart(fd, 0);
177 child = fork();
178 if (child < 0) {
179 printf("fork error\n");
180 PerfStop(fd);
181 return;
182 } else if (child == 0) {
183 (void)execve(cmd.path, cmd.params, NULL);
184 exit(0);
185 }
186
187 waitpid(child, 0, 0);
188 PerfStop(fd);
189
190 buf = (char *)malloc(LOSCFG_PERF_BUFFER_SIZE);
191 if (buf == NULL) {
192 printf("no memory for read perf 0x%x\n", LOSCFG_PERF_BUFFER_SIZE);
193 return;
194 }
195 len = PerfRead(fd, buf, LOSCFG_PERF_BUFFER_SIZE);
196 ret = PerfWriteFile(g_savePath, buf, len);
197 if (ret == 0) {
198 printf("save perf data success at %s\n", g_savePath);
199 } else {
200 printf("save perf data failed at %s\n", g_savePath);
201 }
202 free(buf);
203 }
204