更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
perf_record.c
浏览该文件的文档.
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
47static const char *g_savePath = "/storage/data/perf.data";
48
49static inline int GetEvents(const char *argv)
50{
52}
53
54static inline int GetTids(const char *argv)
55{
56 return ParseIds(argv, (int *)g_recordAttr.taskIds, &g_recordAttr.taskIdsNr);
57}
58
59static inline int GetPids(const char *argv)
60{
62}
63
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
74static int PerfRecordAttrInit(void)
75{
76 PerfConfigAttr attr = {
77 .eventsCfg = {
78#ifdef LOSCFG_PERF_HW_PMU
80 .events = {
81 [0] = {PERF_COUNT_HW_CPU_CYCLES, 0xFFFF},
82 },
83#elif defined LOSCFG_PERF_TIMED_PMU
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 = {
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,
103 };
104
105 return memcpy_s(&g_recordAttr, sizeof(PerfConfigAttr), &attr, sizeof(PerfConfigAttr)) != EOK ? -1 : 0;
106}
107
108ssize_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
146void 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}
void PerfStart(int fd, size_t sectionId)
Definition: perf.c:106
void PerfPrintBuffer(const char *buf, ssize_t num)
Definition: perf.c:70
int PerfConfig(int fd, PerfConfigAttr *attr)
Definition: perf.c:116
ssize_t PerfRead(int fd, char *buf, size_t size)
Definition: perf.c:126
void PerfStop(int fd)
Definition: perf.c:111
@ PERF_COUNT_CPU_CLOCK
Definition: perf.h:85
@ PERF_EVENT_TYPE_SW
Definition: perf.h:59
@ PERF_EVENT_TYPE_HW
Definition: perf.h:57
@ PERF_EVENT_TYPE_TIMED
Definition: perf.h:58
@ PERF_COUNT_SW_TASK_SWITCH
Definition: perf.h:92
@ PERF_COUNT_HW_CPU_CYCLES
Definition: perf.h:69
@ PERF_RECORD_CALLCHAIN
Definition: perf.h:111
@ PERF_RECORD_IP
Definition: perf.h:110
INT64 ssize_t
Definition: los_typedef.h:79
void * malloc(size_t size)
动态分配内存块大小
Definition: malloc.c:81
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
int ParseIds(const char *argv, int *arr, unsigned int *len)
Definition: option.c:102
int ParseEvents(const char *argv, PerfEventConfig *eventsCfg, unsigned int *len)
Definition: option.c:144
int ParseOptions(int argc, char **argv, PerfOption *opt, SubCmd *cmd)
Definition: option.c:71
static int GetTids(const char *argv)
Definition: perf_record.c:54
static int PerfRecordAttrInit(void)
Definition: perf_record.c:74
static int GetEvents(const char *argv)
Definition: perf_record.c:49
static PerfConfigAttr g_recordAttr
Definition: perf_record.c:46
static int GetPids(const char *argv)
Definition: perf_record.c:59
static const char * g_savePath
Definition: perf_record.c:47
void PerfRecord(int fd, int argc, char **argv)
Definition: perf_record.c:146
static PerfOption g_recordOpts[]
Definition: perf_record.c:64
ssize_t PerfWriteFile(const char *filePath, const char *buf, ssize_t bufSize)
Definition: perf_record.c:108
void exit(int status)
Definition: stdlib.c:60
unsigned int processIdsNr
Definition: perf.h:143
PerfEventConfig eventsCfg
Definition: perf.h:138
unsigned int processIds[PERF_MAX_FILTER_TSKS]
Definition: perf.h:142
unsigned int taskIds[PERF_MAX_FILTER_TSKS]
Definition: perf.h:139
unsigned int taskIdsNr
Definition: perf.h:140
unsigned int sampleType
Definition: perf.h:145
unsigned int period
Definition: perf.h:124
unsigned int eventsNr
Definition: perf.h:127
unsigned int type
Definition: perf.h:121
size_t predivided
Definition: perf.h:128
struct PerfEventConfig::@0 events[PERF_MAX_EVENT]
Definition: option.h:61
const char * path
Definition: option.h:62
char * params[CMD_MAX_PARAMS]
Definition: option.h:63
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void