-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The profile provider supports profiling by allowing the user to specify how many times it will fire pre-second. Values of 1-1000 are supported, and the profile provider supports two probe formats as it's original implementation commit 12b8ca2("profile provider support"). * profile:[N]hz: Profile on all CPUs N times per second * profile:[C]:[N]hz: Profile on CPU C N times per second Test this changes with 4 cores x86-64 PC as below. $ sudo ./ply 'profile:2:100hz { @[cpu]=count(); } i:1s { print(@); }' @: { 2 }: 100 @: { 2 }: 200 @: { 2 }: 300 $ sudo ./ply 'profile:100hz { @[cpu]=count(); } i:1s { print(@); }' @: { 0 }: 100 { 1 }: 100 { 2 }: 100 { 3 }: 100 @: { 0 }: 200 { 1 }: 200 { 2 }: 200 { 3 }: 200 @: { 0 }: 300 { 1 }: 300 { 2 }: 300 { 3 }: 300 Change-Id: Ie669db20ed52947f5c75bf91c34f385a64029b19 Signed-off-by: Ism Hong <[email protected]>
- Loading branch information
Showing
7 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright Ism Hong <[email protected]> | ||
* | ||
* SPDX-License-Identifier: GPL-2.0 | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <errno.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include <ply/ply.h> | ||
#include <ply/internal.h> | ||
|
||
|
||
struct profile_data { | ||
int cpu; | ||
int ncpus; | ||
unsigned long long freq; | ||
int *evfds; | ||
}; | ||
|
||
static int profile_sym_alloc(struct ply_probe *pb, struct node *n) | ||
{ | ||
return -ENOENT; | ||
} | ||
|
||
static int profile_probe(struct ply_probe *pb) | ||
{ | ||
int cpu = -1, ncpus = 0; | ||
struct profile_data *data; | ||
int freq = -1; | ||
|
||
/* | ||
* Expected format is either profile:[n]hz where n is a number between | ||
* 1 and 1000, or profile:[c]:[n]hz where c is the CPU to profile. | ||
*/ | ||
if (sscanf(pb->probe, "profile:%d:%dhz", &cpu, &freq) != 2) { | ||
cpu = -1; | ||
if (sscanf(pb->probe, "profile:%dhz", &freq) != 1) | ||
return -EINVAL; | ||
} | ||
|
||
if (freq < 0 || freq > 1000) | ||
return -EINVAL; | ||
|
||
ncpus = sysconf(_SC_NPROCESSORS_ONLN); | ||
|
||
if (cpu < -1 || cpu > ncpus) | ||
return -EINVAL; | ||
|
||
if (cpu >= 0) | ||
ncpus = 1; | ||
|
||
data = calloc(1, sizeof(*data)); | ||
if (!data) | ||
return -ENOMEM; | ||
|
||
data->evfds = calloc(ncpus, sizeof (int)); | ||
if (!data->evfds) { | ||
free(data); | ||
return -ENOMEM; | ||
} | ||
|
||
data->freq = (unsigned long long)freq; | ||
data->cpu = cpu; | ||
data->ncpus = ncpus ; | ||
|
||
pb->provider_data = data; | ||
return 0; | ||
} | ||
|
||
static int profile_attach(struct ply_probe *pb) | ||
{ | ||
struct profile_data *data = pb->provider_data; | ||
int cpu; | ||
|
||
if (data->cpu != -1) { | ||
data->evfds[0] = perf_event_attach_profile(pb, data->cpu, | ||
data->freq); | ||
if (data->evfds[0] < 0) { | ||
_e("%s: Unable to attach profile probe: %s\n", | ||
pb->probe, strerror(errno)); | ||
return data->evfds[0]; | ||
} | ||
} else { | ||
for (cpu = 0; cpu < data->ncpus; cpu++) { | ||
data->evfds[cpu] = perf_event_attach_profile(pb, cpu, data->freq); | ||
if (data->evfds[cpu] < 0) { | ||
_e("%s: Unable to attach profile probe: %s\n", | ||
pb->probe, strerror(errno)); | ||
return data->evfds[cpu]; | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static int profile_detach(struct ply_probe *pb) | ||
{ | ||
struct profile_data *data = pb->provider_data; | ||
|
||
for (int i = 0; i < data->ncpus; i++) { | ||
if (data->evfds[i] > 0) | ||
close(data->evfds[i]); | ||
} | ||
free(data->evfds); | ||
free(data); | ||
|
||
return 0; | ||
} | ||
|
||
struct provider profile = { | ||
.name = "profile", | ||
.prog_type = BPF_PROG_TYPE_PERF_EVENT, | ||
|
||
.sym_alloc = profile_sym_alloc, | ||
.probe = profile_probe, | ||
|
||
.attach = profile_attach, | ||
.detach = profile_detach, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters