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 <stdio.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/ioctl.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include "perf.h"
39
40 #define PERF_IOC_MAGIC 'T'
41 #define PERF_START _IO(PERF_IOC_MAGIC, 1)
42 #define PERF_STOP _IO(PERF_IOC_MAGIC, 2)
43
PerfUsage(void)44 void PerfUsage(void)
45 {
46 printf("\nUsage: ./perf start [id]. Start perf.\n");
47 printf("\nUsage: ./perf stop. Stop perf.\n");
48 printf("\nUsage: ./perf read <nBytes>. Read nBytes raw data from perf buffer and print out.\n");
49 printf("\nUsage: ./perf list. List events to be used in -e.\n");
50 printf("\nUsage: ./perf stat/record [option] <command>. \n"
51 "-e, event selector. use './perf list' to list available events.\n"
52 "-p, event period.\n"
53 "-o, perf data output filename.\n"
54 "-t, taskId filter(allowlist), if not set perf will sample all tasks.\n"
55 "-s, type of data to sample defined in PerfSampleType los_perf.h.\n"
56 "-P, processId filter(allowlist), if not set perf will sample all processes.\n"
57 "-d, whether to prescaler (once every 64 counts),"
58 "which only take effect on cpu cycle hardware event.\n"
59 );
60 }
61
PerfSetPeriod(PerfConfigAttr * attr)62 static void PerfSetPeriod(PerfConfigAttr *attr)
63 {
64 int i;
65 for (i = 1; i < attr->eventsCfg.eventsNr; i++) {
66 attr->eventsCfg.events[i].period = attr->eventsCfg.events[0].period;
67 }
68 }
69
PerfPrintBuffer(const char * buf,ssize_t num)70 void PerfPrintBuffer(const char *buf, ssize_t num)
71 {
72 #define BYTES_PER_LINE 4
73 ssize_t i;
74 for (i = 0; i < num; i++) {
75 printf(" %02x", (unsigned char)buf[i]);
76 if (((i + 1) % BYTES_PER_LINE) == 0) {
77 printf("\n");
78 }
79 }
80 printf("\n");
81 }
82
PerfDumpAttr(PerfConfigAttr * attr)83 void PerfDumpAttr(PerfConfigAttr *attr)
84 {
85 int i;
86 printf_debug("attr->type: %d\n", attr->eventsCfg.type);
87 for (i = 0; i < attr->eventsCfg.eventsNr; i++) {
88 printf_debug("attr->events[%d]: %d, 0x%x\n", i, attr->eventsCfg.events[i].eventId,
89 attr->eventsCfg.events[i].period);
90 }
91 printf_debug("attr->predivided: %d\n", attr->eventsCfg.predivided);
92 printf_debug("attr->sampleType: 0x%x\n", attr->sampleType);
93
94 for (i = 0; i < attr->taskIdsNr; i++) {
95 printf_debug("attr->taskIds[%d]: %d\n", i, attr->taskIds[i]);
96 }
97
98 for (i = 0; i < attr->processIdsNr; i++) {
99 printf_debug("attr->processIds[%d]: %d\n", i, attr->processIds[i]);
100 }
101
102 printf_debug("attr->needSample: %d\n", attr->needSample);
103 }
104
105
PerfStart(int fd,size_t sectionId)106 void PerfStart(int fd, size_t sectionId)
107 {
108 (void)ioctl(fd, PERF_START, sectionId);
109 }
110
PerfStop(int fd)111 void PerfStop(int fd)
112 {
113 (void)ioctl(fd, PERF_STOP, NULL);
114 }
115
PerfConfig(int fd,PerfConfigAttr * attr)116 int PerfConfig(int fd, PerfConfigAttr *attr)
117 {
118 if (attr == NULL) {
119 return -1;
120 }
121 PerfSetPeriod(attr);
122 PerfDumpAttr(attr);
123 return write(fd, attr, sizeof(PerfConfigAttr));
124 }
125
PerfRead(int fd,char * buf,size_t size)126 ssize_t PerfRead(int fd, char *buf, size_t size)
127 {
128 ssize_t len;
129 if (buf == NULL) {
130 printf("Read buffer is null.\n");
131 return 0;
132 }
133
134 len = read(fd, buf, size);
135 return len;
136 }