Skip to content

Commit

Permalink
Merge branch 'resctrl-covscan'
Browse files Browse the repository at this point in the history
  • Loading branch information
natoscott committed Nov 4, 2023
2 parents 3b293d7 + 70be95a commit 348ccae
Showing 1 changed file with 61 additions and 76 deletions.
137 changes: 61 additions & 76 deletions src/pmdas/resctrl/resctrl.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* Llc, configurable PMDA
* Linux kernel Resource Control PMDA
*
* Copyright (c) 2023 Red Hat.
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
Expand All @@ -24,7 +24,7 @@
#include <stdio.h>

/*
* RESCTRL LLC Last-Level Cache of CPU info.
* RESCTRL LLC (Last-Level Cache) of CPU info.
*
* Linux supports resctrl which can be used to
* control QoS of CPU cache and monitor LLC(last level cache).
Expand Down Expand Up @@ -56,10 +56,6 @@ static pmdaIndom indomtab[] = {
{ LLC_INDOM, 0, NULL }
};

/* occupancy LLC:0:0
mbm_local LLC:0:1
mbm_total LLC:0:2 */

static pmInDom *llc_indom = &indomtab[LLC_INDOM].it_indom;

static pmdaMetric metrictab[] = {
Expand All @@ -69,11 +65,11 @@ static pmdaMetric metrictab[] = {
PMDA_PMUNITS(0,0,0,0,0,0) }, },
// mbm_local
{ NULL,
{ PMDA_PMID(0,1), PM_TYPE_U32, LLC_INDOM, PM_SEM_INSTANT,
{ PMDA_PMID(0,1), PM_TYPE_U64, LLC_INDOM, PM_SEM_INSTANT,
PMDA_PMUNITS(0,0,0,0,0,0) }, },
// mbm_total
{ NULL,
{ PMDA_PMID(0,2), PM_TYPE_U32, LLC_INDOM, PM_SEM_INSTANT,
{ PMDA_PMID(0,2), PM_TYPE_U64, LLC_INDOM, PM_SEM_INSTANT,
PMDA_PMUNITS(0,0,0,0,0,0) }, },
};

Expand All @@ -84,8 +80,8 @@ static char llcdir[MAXPATHLEN];

typedef struct {
float llc_occupancy;
unsigned long llc_mbm_local;
unsigned long llc_mbm_total;
unsigned long long llc_mbm_local;
unsigned long long llc_mbm_total;
} llc_metrics;

/* command line option handling - both short and long options */
Expand Down Expand Up @@ -141,89 +137,78 @@ llc_fetch(int numpmid, pmID pmidlist[], pmResult **resp, pmdaExt *pmda)
DIR *dirp;
FILE *fp;
static char fn[MAXPATHLEN + 1024];
char linebuf[1024];
struct dirent *dentry;
unsigned long llc_occupancy_total;
char linebuf[1024];
struct dirent *dentry;
float llc_occupancy = 0;
unsigned long llc_mbm_local = 0;
unsigned long llc_mbm_total = 0;
unsigned long long llc_mbm_local = 0;
unsigned long long llc_mbm_total = 0;
unsigned long long llc_occupancy_total;
static const char *l3_sys_cache_size = "/sys/devices/system/cpu/cpu0/cache/index3/size";

/*
* gather per LLC related statistics from the file
* /sys/fs/llc/mon_data/mon_L3_XX
*/
dirp = opendir(llcdir);
if (! dirp)
if (!dirp)
return PM_ERR_INST;

static int l3_cache_size;

if (!l3_cache_size)
{
if ( (fp = fopen(l3_sys_cache_size, "r")) != NULL)
{
if ( fgets(linebuf, sizeof(linebuf), fp) != NULL)
{
sscanf(linebuf, "%uK\n", &l3_cache_size);
l3_cache_size *= 1024;
}
fclose(fp);
}
if (!l3_cache_size) {
if ((fp = fopen(l3_sys_cache_size, "r")) != NULL) {
if (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
sscanf(linebuf, "%uK\n", &l3_cache_size);
l3_cache_size *= 1024;
}
fclose(fp);
}

}

/*
* walk the LLC directory, gather each LLC
*/
while ( (dentry = readdir(dirp)) )
{
if (strncmp(dentry->d_name, "mon_L3_", 7))
continue;

pmsprintf(fn, sizeof fn, "%s/%s/llc_occupancy", llcdir, dentry->d_name);
if ( (fp = fopen(fn, "r")) != NULL)
{
if ( fgets(linebuf, sizeof(linebuf), fp) != NULL)
{
sscanf(linebuf, "%lu\n", &llc_occupancy_total);
llc_occupancy = (float)llc_occupancy_total / l3_cache_size;
}
fclose(fp);
}

pmsprintf(fn, sizeof fn, "%s/%s/mbm_local_bytes", llcdir, dentry->d_name);
if ( (fp = fopen(fn, "r")) != NULL)
{
if ( fgets(linebuf, sizeof(linebuf), fp) != NULL)
{
sscanf(linebuf, "%lu\n", &llc_mbm_local);
}
fclose(fp);
}

pmsprintf(fn, sizeof fn, "%s/%s/mbm_total_bytes", llcdir, dentry->d_name);
if ( (fp = fopen(fn, "r")) != NULL)
{
if ( fgets(linebuf, sizeof(linebuf), fp) != NULL)
{
sscanf(linebuf, "%lu\n", &llc_mbm_total);
}
fclose(fp);
}

llc_metrics *lspm;
int sts = pmdaCacheLookupName(*llc_indom, dentry->d_name, NULL, (void **)&lspm);
if (sts < 0 || lspm == NULL) {
if ((lspm = calloc(1, sizeof(*lspm))) == NULL)
return PM_ERR_INST;
while ((dentry = readdir(dirp))) {
if (strncmp(dentry->d_name, "mon_L3_", 7))
continue;

pmsprintf(fn, sizeof fn, "%s/%s/llc_occupancy", llcdir, dentry->d_name);
if ((fp = fopen(fn, "r")) != NULL) {
if (fgets(linebuf, sizeof(linebuf), fp) != NULL && l3_cache_size) {
sscanf(linebuf, "%llu\n", &llc_occupancy_total);
llc_occupancy = (float)llc_occupancy_total / l3_cache_size;
}
fclose(fp);
}

pmsprintf(fn, sizeof fn, "%s/%s/mbm_local_bytes", llcdir, dentry->d_name);
if ((fp = fopen(fn, "r")) != NULL) {
if (fgets(linebuf, sizeof(linebuf), fp) != NULL)
sscanf(linebuf, "%llu\n", &llc_mbm_local);
fclose(fp);
}

pmsprintf(fn, sizeof fn, "%s/%s/mbm_total_bytes", llcdir, dentry->d_name);
if ((fp = fopen(fn, "r")) != NULL) {
if (fgets(linebuf, sizeof(linebuf), fp) != NULL)
sscanf(linebuf, "%llu\n", &llc_mbm_total);
fclose(fp);
}

llc_metrics *lspm = NULL;
int sts = pmdaCacheLookupName(*llc_indom, dentry->d_name, NULL, (void **)&lspm);
if (sts < 0 || lspm == NULL) {
if ((lspm = calloc(1, sizeof(*lspm))) == NULL) {
closedir(dirp);
return PM_ERR_INST;
}
/* store the completed info values into the cached structure */
lspm->llc_occupancy = llc_occupancy;
lspm->llc_mbm_local = llc_mbm_local;
lspm->llc_mbm_total = llc_mbm_total;
pmdaCacheStore(*llc_indom, PMDA_CACHE_ADD, dentry->d_name, (void *)lspm);
}
/* store the completed info values into the cached structure */
lspm->llc_occupancy = llc_occupancy;
lspm->llc_mbm_local = llc_mbm_local;
lspm->llc_mbm_total = llc_mbm_total;
pmdaCacheStore(*llc_indom, PMDA_CACHE_ADD, dentry->d_name, (void *)lspm);
}
closedir(dirp);

return pmdaFetch(numpmid, pmidlist, resp, pmda);
Expand Down

0 comments on commit 348ccae

Please sign in to comment.