Skip to content

Commit 16ce467

Browse files
Patryk Wlazlynlenb
authored andcommitted
tools/power turbostat: Allow mapping multiple PMT files with the same GUID
Some platforms may expose multiple telemetry files identified with the same GUID. Interpreting it correctly, to associate given counter with a CPU, core or a package requires more metadata from the user. Parse and create ordered, linked list of those PMT aggregators, so that we can identify specific aggregator with GUID + sequence number. Signed-off-by: Patryk Wlazlyn <[email protected]> Signed-off-by: Len Brown <[email protected]>
1 parent 4265a86 commit 16ce467

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

tools/power/x86/turbostat/turbostat.c

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9033,46 +9033,35 @@ int parse_telem_info_file(int fd_dir, const char *info_filename, const char *for
90339033

90349034
struct pmt_mmio *pmt_mmio_open(unsigned int target_guid)
90359035
{
9036-
DIR *dirp;
9037-
struct dirent *entry;
9036+
struct pmt_diriter_t pmt_iter;
9037+
const struct dirent *entry;
90389038
struct stat st;
9039-
unsigned int telem_idx;
90409039
int fd_telem_dir, fd_pmt;
90419040
unsigned long guid, size, offset;
90429041
size_t mmap_size;
90439042
void *mmio;
9044-
struct pmt_mmio *ret = NULL;
9043+
struct pmt_mmio *head = NULL, *last = NULL;
9044+
struct pmt_mmio *new_pmt = NULL;
90459045

90469046
if (stat(SYSFS_TELEM_PATH, &st) == -1)
90479047
return NULL;
90489048

9049-
dirp = opendir(SYSFS_TELEM_PATH);
9050-
if (dirp == NULL)
9049+
pmt_diriter_init(&pmt_iter);
9050+
entry = pmt_diriter_begin(&pmt_iter, SYSFS_TELEM_PATH);
9051+
if (!entry) {
9052+
pmt_diriter_remove(&pmt_iter);
90519053
return NULL;
9054+
}
90529055

9053-
for (;;) {
9054-
entry = readdir(dirp);
9055-
9056-
if (entry == NULL)
9057-
break;
9058-
9059-
if (strcmp(entry->d_name, ".") == 0)
9060-
continue;
9061-
9062-
if (strcmp(entry->d_name, "..") == 0)
9063-
continue;
9064-
9065-
if (sscanf(entry->d_name, "telem%u", &telem_idx) != 1)
9066-
continue;
9067-
9068-
if (fstatat(dirfd(dirp), entry->d_name, &st, 0) == -1) {
9056+
for (;entry != NULL; entry = pmt_diriter_next(&pmt_iter)) {
9057+
if (fstatat(dirfd(pmt_iter.dir), entry->d_name, &st, 0) == -1) {
90699058
break;
90709059
}
90719060

90729061
if (!S_ISDIR(st.st_mode))
90739062
continue;
90749063

9075-
fd_telem_dir = openat(dirfd(dirp), entry->d_name, O_RDONLY);
9064+
fd_telem_dir = openat(dirfd(pmt_iter.dir), entry->d_name, O_RDONLY);
90769065
if (fd_telem_dir == -1) {
90779066
break;
90789067
}
@@ -9106,35 +9095,51 @@ struct pmt_mmio *pmt_mmio_open(unsigned int target_guid)
91069095
mmap_size = ROUND_UP_TO_PAGE_SIZE(size);
91079096
mmio = mmap(0, mmap_size, PROT_READ, MAP_SHARED, fd_pmt, 0);
91089097
if (mmio != MAP_FAILED) {
9109-
91109098
if (debug)
91119099
fprintf(stderr, "%s: 0x%lx mmaped at: %p\n", __func__, guid, mmio);
91129100

9113-
ret = calloc(1, sizeof(*ret));
9101+
new_pmt = calloc(1, sizeof(*new_pmt));
91149102

9115-
if (!ret) {
9103+
if (!new_pmt) {
91169104
fprintf(stderr, "%s: Failed to allocate pmt_mmio\n", __func__);
91179105
exit(1);
91189106
}
91199107

9120-
ret->guid = guid;
9121-
ret->mmio_base = mmio;
9122-
ret->pmt_offset = offset;
9123-
ret->size = size;
9108+
/*
9109+
* Create linked list of mmaped regions,
9110+
* but preserve the ordering from sysfs.
9111+
* Ordering is important for the user to
9112+
* use the seq=%u parameter when adding a counter.
9113+
*/
9114+
new_pmt->guid = guid;
9115+
new_pmt->mmio_base = mmio;
9116+
new_pmt->pmt_offset = offset;
9117+
new_pmt->size = size;
9118+
new_pmt->next = pmt_mmios;
9119+
9120+
if (last)
9121+
last->next = new_pmt;
9122+
else
9123+
head = new_pmt;
91249124

9125-
ret->next = pmt_mmios;
9126-
pmt_mmios = ret;
9125+
last = new_pmt;
91279126
}
91289127

91299128
loop_cleanup_and_break:
91309129
close(fd_pmt);
91319130
close(fd_telem_dir);
9132-
break;
91339131
}
91349132

9135-
closedir(dirp);
9133+
pmt_diriter_remove(&pmt_iter);
91369134

9137-
return ret;
9135+
/*
9136+
* If we found something, stick just
9137+
* created linked list to the front.
9138+
*/
9139+
if (head)
9140+
pmt_mmios = head;
9141+
9142+
return head;
91389143
}
91399144

91409145
struct pmt_mmio *pmt_mmio_find(unsigned int guid)

0 commit comments

Comments
 (0)