2013-03-11 11:30:00 +05:30
|
|
|
/*
|
2016-06-05 10:30:00 +05:30
|
|
|
* slabinfo.c - slab related functions for libproc
|
2004-01-25 04:03:56 +05:30
|
|
|
*
|
|
|
|
* Chris Rivera <cmrivera@ufl.edu>
|
|
|
|
* Robert Love <rml@tech9.net>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Chris Rivera
|
2005-01-10 11:29:41 +05:30
|
|
|
* Copyright 2004, Albert Cahalan
|
2015-07-11 10:30:00 +05:30
|
|
|
* Copyright (C) 2015 Craig Small <csmall@enc.com.au>
|
2012-03-02 17:59:36 +05:30
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-01-25 04:03:56 +05:30
|
|
|
*/
|
|
|
|
|
2004-07-19 10:26:10 +05:30
|
|
|
#include <ctype.h>
|
2015-07-04 10:29:59 +05:30
|
|
|
#include <errno.h>
|
2015-07-21 10:30:00 +05:30
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-07-04 10:29:59 +05:30
|
|
|
#include <string.h>
|
2015-07-21 10:30:00 +05:30
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-07-04 10:29:59 +05:30
|
|
|
#include <sys/stat.h>
|
2015-07-21 10:30:00 +05:30
|
|
|
#include <sys/types.h>
|
2004-01-25 04:03:56 +05:30
|
|
|
|
2016-06-05 10:30:00 +05:30
|
|
|
#include <proc/slabinfo.h>
|
2015-07-04 10:29:59 +05:30
|
|
|
#include "procps-private.h"
|
|
|
|
|
|
|
|
#define SLABINFO_FILE "/proc/slabinfo"
|
2015-07-11 10:30:00 +05:30
|
|
|
#define SLABINFO_LINE_LEN 2048
|
|
|
|
#define SLAB_INFO_NAME_LEN 128
|
2015-07-04 10:29:59 +05:30
|
|
|
|
|
|
|
struct slabinfo_stats {
|
|
|
|
unsigned long total_size; /* size of all objects */
|
|
|
|
unsigned long active_size; /* size of all active objects */
|
2015-07-21 10:30:00 +05:30
|
|
|
unsigned int nr_objs; /* number of objects, among all caches */
|
|
|
|
unsigned int nr_active_objs; /* number of active objects, among all caches */
|
|
|
|
unsigned int nr_pages; /* number of pages consumed by all objects */
|
|
|
|
unsigned int nr_slabs; /* number of slabs, among all caches */
|
|
|
|
unsigned int nr_active_slabs; /* number of active slabs, among all caches */
|
|
|
|
unsigned int nr_caches; /* number of caches */
|
|
|
|
unsigned int nr_active_caches; /* number of active caches */
|
|
|
|
unsigned int avg_obj_size; /* average object size */
|
|
|
|
unsigned int min_obj_size; /* size of smallest object */
|
|
|
|
unsigned int max_obj_size; /* size of largest object */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct slabinfo_node {
|
|
|
|
char name[SLAB_INFO_NAME_LEN]; /* name of this cache */
|
|
|
|
unsigned long cache_size; /* size of entire cache */
|
|
|
|
unsigned int nr_objs; /* number of objects in this cache */
|
|
|
|
unsigned int nr_active_objs; /* number of active objects */
|
|
|
|
unsigned int obj_size; /* size of each object */
|
|
|
|
unsigned int objs_per_slab; /* number of objects per slab */
|
|
|
|
unsigned int pages_per_slab; /* number of pages per slab */
|
|
|
|
unsigned int nr_slabs; /* number of slabs in this cache */
|
|
|
|
unsigned int nr_active_slabs; /* number of active slabs */
|
|
|
|
unsigned int use; /* percent full: total / active */
|
2015-07-04 10:29:59 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
struct procps_slabinfo {
|
|
|
|
int refcount;
|
|
|
|
FILE *slabinfo_fp;
|
|
|
|
struct slabinfo_stats stats;
|
2015-07-11 10:30:00 +05:30
|
|
|
struct slabinfo_node *nodes; /* first slabnode of this list */
|
2015-07-04 10:29:59 +05:30
|
|
|
int nodes_alloc; /* nodes alloc()ed */
|
|
|
|
int nodes_used; /* nodes using alloced memory */
|
2015-07-21 10:30:00 +05:30
|
|
|
struct stacks_anchor *stacked;
|
2015-07-11 10:30:00 +05:30
|
|
|
};
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
struct stack_vectors {
|
|
|
|
struct stacks_anchor *owner;
|
|
|
|
struct slabnode_stack **heads;
|
2015-07-11 10:30:00 +05:30
|
|
|
};
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
struct stacks_anchor {
|
2015-07-11 10:30:00 +05:30
|
|
|
int depth;
|
|
|
|
int inuse;
|
2015-07-21 10:30:00 +05:30
|
|
|
struct stack_vectors *vectors;
|
|
|
|
struct stacks_anchor *self;
|
|
|
|
struct stacks_anchor *next;
|
2015-07-04 10:29:59 +05:30
|
|
|
};
|
2004-01-25 04:03:56 +05:30
|
|
|
|
2015-07-11 10:30:00 +05:30
|
|
|
|
2004-01-25 04:03:56 +05:30
|
|
|
/*
|
2015-07-04 10:29:59 +05:30
|
|
|
* Zero out the slabnode data, keeping the memory allocated.
|
2004-01-25 04:03:56 +05:30
|
|
|
*/
|
2015-07-11 10:30:00 +05:30
|
|
|
static void slabnodes_clear (
|
2015-07-04 10:29:59 +05:30
|
|
|
struct procps_slabinfo *info)
|
2004-01-25 04:03:56 +05:30
|
|
|
{
|
2015-07-04 10:29:59 +05:30
|
|
|
if (info == NULL || info->nodes == NULL || info->nodes_alloc < 1)
|
|
|
|
return;
|
2015-07-11 10:30:00 +05:30
|
|
|
memset(info->nodes, 0, sizeof(struct slabinfo_node)*info->nodes_alloc);
|
2015-07-04 10:29:59 +05:30
|
|
|
info->nodes_used = 0;
|
2004-01-25 04:03:56 +05:30
|
|
|
}
|
|
|
|
|
2015-07-04 10:29:59 +05:30
|
|
|
/* Alloc up more slabnode memory, if required
|
2004-03-27 10:00:05 +05:30
|
|
|
*/
|
2015-07-11 10:30:00 +05:30
|
|
|
static int slabnodes_alloc (
|
2015-07-04 10:29:59 +05:30
|
|
|
struct procps_slabinfo *info)
|
2004-03-27 10:00:05 +05:30
|
|
|
{
|
2015-07-11 10:30:00 +05:30
|
|
|
struct slabinfo_node *new_nodes;
|
2015-07-04 10:29:59 +05:30
|
|
|
int new_count;
|
|
|
|
|
|
|
|
if (info == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (info->nodes_used < info->nodes_alloc)
|
|
|
|
return 0;
|
|
|
|
/* Increment the allocated number of slabs */
|
|
|
|
new_count = info->nodes_alloc * 5/4+30;
|
|
|
|
|
2015-07-11 10:30:00 +05:30
|
|
|
new_nodes = realloc(info->nodes, sizeof(struct slabinfo_node) * new_count);
|
|
|
|
if (!new_nodes)
|
2015-07-04 10:29:59 +05:30
|
|
|
return -ENOMEM;
|
|
|
|
info->nodes = new_nodes;
|
|
|
|
info->nodes_alloc = new_count;
|
|
|
|
return 0;
|
2004-03-27 10:00:05 +05:30
|
|
|
}
|
|
|
|
|
2004-01-25 04:03:56 +05:30
|
|
|
/*
|
2015-07-04 10:29:59 +05:30
|
|
|
* get_slabnode - allocate slab_info structures using a free list
|
|
|
|
*
|
|
|
|
* In the fast path, we simply return a node off the free list. In the slow
|
|
|
|
* list, we malloc() a new node. The free list is never automatically reaped,
|
|
|
|
* both for simplicity and because the number of slab caches is fairly
|
|
|
|
* constant.
|
2004-01-25 04:03:56 +05:30
|
|
|
*/
|
2015-07-11 10:30:00 +05:30
|
|
|
static int get_slabnode (
|
2015-07-04 10:29:59 +05:30
|
|
|
struct procps_slabinfo *info,
|
2015-07-11 10:30:00 +05:30
|
|
|
struct slabinfo_node **node)
|
2004-01-25 04:03:56 +05:30
|
|
|
{
|
2015-07-04 10:29:59 +05:30
|
|
|
int retval;
|
2004-01-25 04:03:56 +05:30
|
|
|
|
2015-07-04 10:29:59 +05:30
|
|
|
if (!info)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (info->nodes_used == info->nodes_alloc) {
|
|
|
|
if ((retval = slabnodes_alloc(info)) < 0)
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
*node = &(info->nodes[info->nodes_used++]);
|
|
|
|
return 0;
|
2004-01-25 04:03:56 +05:30
|
|
|
}
|
|
|
|
|
2015-07-04 10:29:59 +05:30
|
|
|
/* parse_slabinfo20:
|
|
|
|
*
|
|
|
|
* sactual parse routine for slabinfo 2.x (2.6 kernels)
|
2013-03-26 14:11:03 +05:30
|
|
|
* Note: difference between 2.0 and 2.1 is in the ": globalstat" part where version 2.1
|
|
|
|
* has extra column <nodeallocs>. We don't use ": globalstat" part in both versions.
|
|
|
|
*
|
|
|
|
* Formats (we don't use "statistics" extensions)
|
|
|
|
*
|
|
|
|
* slabinfo - version: 2.1
|
|
|
|
* # name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> \
|
|
|
|
* : tunables <batchcount> <limit> <sharedfactor> \
|
|
|
|
* : slabdata <active_slabs> <num_slabs> <sharedavail>
|
|
|
|
*
|
|
|
|
* slabinfo - version: 2.1 (statistics)
|
|
|
|
* # name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> \
|
|
|
|
* : tunables <batchcount> <limit> <sharedfactor> \
|
|
|
|
* : slabdata <active_slabs> <num_slabs> <sharedavail> \
|
|
|
|
* : globalstat <listallocs> <maxobjs> <grown> <reaped> <error> <maxfreeable> <freelimit> <nodeallocs> \
|
|
|
|
* : cpustat <allochit> <allocmiss> <freehit> <freemiss>
|
|
|
|
*
|
|
|
|
* slabinfo - version: 2.0
|
|
|
|
* # name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> \
|
|
|
|
* : tunables <batchcount> <limit> <sharedfactor> \
|
|
|
|
* : slabdata <active_slabs> <num_slabs> <sharedavail>
|
|
|
|
*
|
|
|
|
* slabinfo - version: 2.0 (statistics)
|
|
|
|
* # name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> \
|
|
|
|
* : tunables <batchcount> <limit> <sharedfactor> \
|
|
|
|
* : slabdata <active_slabs> <num_slabs> <sharedavail> \
|
|
|
|
* : globalstat <listallocs> <maxobjs> <grown> <reaped> <error> <maxfreeable> <freelimit> \
|
|
|
|
* : cpustat <allochit> <allocmiss> <freehit> <freemiss>
|
|
|
|
*/
|
2015-07-11 10:30:00 +05:30
|
|
|
static int parse_slabinfo20 (
|
2015-07-04 10:29:59 +05:30
|
|
|
struct procps_slabinfo *info)
|
2004-01-25 04:03:56 +05:30
|
|
|
{
|
2015-07-11 10:30:00 +05:30
|
|
|
struct slabinfo_node *node;
|
2015-07-04 10:29:59 +05:30
|
|
|
char buffer[SLABINFO_LINE_LEN];
|
|
|
|
int retval;
|
|
|
|
int page_size = getpagesize();
|
|
|
|
struct slabinfo_stats *stats = &(info->stats);
|
|
|
|
|
|
|
|
stats->min_obj_size = INT_MAX;
|
|
|
|
stats->max_obj_size = 0;
|
|
|
|
|
|
|
|
while (fgets(buffer, SLABINFO_LINE_LEN, info->slabinfo_fp )) {
|
|
|
|
if (buffer[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((retval = get_slabnode(info, &node)) < 0)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
if (sscanf(buffer,
|
2015-07-21 10:30:00 +05:30
|
|
|
"%" STRINGIFY(SLAB_INFO_NAME_LEN) "s" \
|
|
|
|
"%u %u %u %u %u : tunables %*u %*u %*u : slabdata %u %u %*u",
|
|
|
|
node->name,
|
2015-07-04 10:29:59 +05:30
|
|
|
&node->nr_active_objs, &node->nr_objs,
|
|
|
|
&node->obj_size, &node->objs_per_slab,
|
|
|
|
&node->pages_per_slab, &node->nr_active_slabs,
|
|
|
|
&node->nr_slabs) < 8) {
|
|
|
|
if (errno != 0)
|
|
|
|
return -errno;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-07-11 10:30:00 +05:30
|
|
|
if (!node->name[0])
|
|
|
|
snprintf(node->name, sizeof(node->name), "%s", "unknown");
|
|
|
|
|
2015-07-04 10:29:59 +05:30
|
|
|
if (node->obj_size < stats->min_obj_size)
|
|
|
|
stats->min_obj_size = node->obj_size;
|
|
|
|
if (node->obj_size > stats->max_obj_size)
|
|
|
|
stats->max_obj_size = node->obj_size;
|
|
|
|
|
|
|
|
node->cache_size = (unsigned long)node->nr_slabs * node->pages_per_slab
|
|
|
|
* page_size;
|
|
|
|
|
|
|
|
if (node->nr_objs) {
|
2016-04-25 15:54:20 +05:30
|
|
|
node->use = (unsigned int)100 * (node->nr_active_objs / node->nr_objs);
|
2015-07-04 10:29:59 +05:30
|
|
|
stats->nr_active_caches++;
|
|
|
|
} else
|
|
|
|
node->use = 0;
|
|
|
|
|
|
|
|
stats->nr_objs += node->nr_objs;
|
|
|
|
stats->nr_active_objs += node->nr_active_objs;
|
|
|
|
stats->total_size += (unsigned long)node->nr_objs * node->obj_size;
|
|
|
|
stats->active_size += (unsigned long)node->nr_active_objs * node->obj_size;
|
|
|
|
stats->nr_pages += node->nr_slabs * node->pages_per_slab;
|
|
|
|
stats->nr_slabs += node->nr_slabs;
|
|
|
|
stats->nr_active_slabs += node->nr_active_slabs;
|
|
|
|
stats->nr_caches++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stats->nr_objs)
|
|
|
|
stats->avg_obj_size = stats->total_size / stats->nr_objs;
|
|
|
|
|
|
|
|
return 0;
|
2004-01-25 04:03:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-07-11 10:30:00 +05:30
|
|
|
* procps_slabinfo_new():
|
2004-01-25 04:03:56 +05:30
|
|
|
*
|
2015-07-04 10:29:59 +05:30
|
|
|
* @info: location of returned new structure
|
|
|
|
*
|
|
|
|
* Returns: 0 on success <0 on failure
|
|
|
|
*/
|
2015-07-11 10:30:00 +05:30
|
|
|
PROCPS_EXPORT int procps_slabinfo_new (
|
2015-07-04 10:29:59 +05:30
|
|
|
struct procps_slabinfo **info)
|
|
|
|
{
|
|
|
|
struct procps_slabinfo *si;
|
|
|
|
|
|
|
|
if (info == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
si = calloc(1, sizeof(struct procps_slabinfo));
|
|
|
|
if (!si)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
si->refcount = 1;
|
|
|
|
si->slabinfo_fp = NULL;
|
|
|
|
si->nodes_alloc = 0;
|
|
|
|
si->nodes_used = 0;
|
|
|
|
si->nodes = NULL;
|
|
|
|
*info = si;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-11 10:30:00 +05:30
|
|
|
/* procps_slabinfo_read():
|
2015-07-04 10:29:59 +05:30
|
|
|
*
|
|
|
|
* Read the data out of /proc/slabinfo putting the information
|
2015-07-21 10:30:00 +05:30
|
|
|
* into the supplied info container
|
2015-07-04 10:29:59 +05:30
|
|
|
*
|
|
|
|
* Returns: 0 on success, negative on error
|
2004-01-25 04:03:56 +05:30
|
|
|
*/
|
2015-07-04 10:29:59 +05:30
|
|
|
PROCPS_EXPORT int procps_slabinfo_read (
|
|
|
|
struct procps_slabinfo *info)
|
|
|
|
{
|
|
|
|
char line[SLABINFO_LINE_LEN];
|
2015-07-11 10:30:00 +05:30
|
|
|
int retval, major, minor;
|
2015-07-04 10:29:59 +05:30
|
|
|
|
|
|
|
if (info == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(&(info->stats), 0, sizeof(struct slabinfo_stats));
|
|
|
|
if ((retval = slabnodes_alloc(info)) < 0)
|
|
|
|
return retval;
|
2015-07-11 10:30:00 +05:30
|
|
|
|
2015-07-04 10:29:59 +05:30
|
|
|
slabnodes_clear(info);
|
|
|
|
|
|
|
|
if (NULL == info->slabinfo_fp &&
|
|
|
|
(info->slabinfo_fp = fopen(SLABINFO_FILE, "r")) == NULL)
|
|
|
|
return -errno;
|
2015-07-11 10:30:00 +05:30
|
|
|
|
2015-07-04 10:29:59 +05:30
|
|
|
if (fseek(info->slabinfo_fp, 0L, SEEK_SET) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
/* Parse the version string */
|
|
|
|
if (!fgets(line, SLABINFO_LINE_LEN, info->slabinfo_fp))
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (sscanf(line, "slabinfo - version: %d.%d", &major, &minor) != 2)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (major == 2)
|
|
|
|
retval = parse_slabinfo20(info);
|
|
|
|
else
|
|
|
|
return -ERANGE;
|
2015-07-11 10:30:00 +05:30
|
|
|
|
2015-07-04 10:29:59 +05:30
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCPS_EXPORT int procps_slabinfo_ref (
|
|
|
|
struct procps_slabinfo *info)
|
2004-01-25 04:03:56 +05:30
|
|
|
{
|
2015-07-04 10:29:59 +05:30
|
|
|
if (info == NULL)
|
|
|
|
return -EINVAL;
|
2015-07-21 10:30:00 +05:30
|
|
|
|
2015-07-04 10:29:59 +05:30
|
|
|
info->refcount++;
|
|
|
|
return info->refcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCPS_EXPORT int procps_slabinfo_unref (
|
|
|
|
struct procps_slabinfo **info)
|
|
|
|
{
|
|
|
|
if (info == NULL || *info == NULL)
|
|
|
|
return -EINVAL;
|
2015-07-21 10:30:00 +05:30
|
|
|
|
2015-07-04 10:29:59 +05:30
|
|
|
(*info)->refcount--;
|
|
|
|
if ((*info)->refcount == 0) {
|
|
|
|
if ((*info)->slabinfo_fp) {
|
|
|
|
fclose((*info)->slabinfo_fp);
|
|
|
|
(*info)->slabinfo_fp = NULL;
|
|
|
|
}
|
2015-07-21 10:30:00 +05:30
|
|
|
if ((*info)->stacked) {
|
2015-07-11 10:30:00 +05:30
|
|
|
do {
|
2015-07-21 10:30:00 +05:30
|
|
|
struct stacks_anchor *p = (*info)->stacked;
|
|
|
|
(*info)->stacked = (*info)->stacked->next;
|
2015-07-11 10:30:00 +05:30
|
|
|
free(p);
|
2015-07-21 10:30:00 +05:30
|
|
|
} while((*info)->stacked);
|
2015-07-11 10:30:00 +05:30
|
|
|
}
|
|
|
|
free((*info)->nodes);
|
2015-07-04 10:29:59 +05:30
|
|
|
free(*info);
|
|
|
|
*info = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (*info)->refcount;
|
|
|
|
}
|
|
|
|
|
2015-07-11 10:30:00 +05:30
|
|
|
PROCPS_EXPORT unsigned long procps_slabs_get (
|
2015-07-04 10:29:59 +05:30
|
|
|
struct procps_slabinfo *info,
|
2015-07-11 10:30:00 +05:30
|
|
|
enum slabs_item item)
|
2015-07-04 10:29:59 +05:30
|
|
|
{
|
2015-07-21 10:30:00 +05:30
|
|
|
/* note: most of the results we might return are actually just
|
|
|
|
unsigned int, but we must accommodate the largest potential
|
|
|
|
result and so return an unsigned long */
|
2015-07-11 10:30:00 +05:30
|
|
|
if (info == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-07-04 10:29:59 +05:30
|
|
|
switch (item) {
|
2015-07-11 10:30:00 +05:30
|
|
|
case PROCPS_SLABS_OBJS:
|
|
|
|
return info->stats.nr_objs;
|
|
|
|
case PROCPS_SLABS_AOBJS:
|
|
|
|
return info->stats.nr_active_objs;
|
|
|
|
case PROCPS_SLABS_PAGES:
|
|
|
|
return info->stats.nr_pages;
|
|
|
|
case PROCPS_SLABS_SLABS:
|
|
|
|
return info->stats.nr_slabs;
|
|
|
|
case PROCPS_SLABS_ASLABS:
|
|
|
|
return info->stats.nr_active_slabs;
|
|
|
|
case PROCPS_SLABS_CACHES:
|
|
|
|
return info->stats.nr_caches;
|
|
|
|
case PROCPS_SLABS_ACACHES:
|
|
|
|
return info->stats.nr_active_caches;
|
|
|
|
case PROCPS_SLABS_SIZE_AVG:
|
|
|
|
return info->stats.avg_obj_size;
|
|
|
|
case PROCPS_SLABS_SIZE_MIN:
|
|
|
|
return info->stats.min_obj_size;
|
|
|
|
case PROCPS_SLABS_SIZE_MAX:
|
|
|
|
return info->stats.max_obj_size;
|
|
|
|
case PROCPS_SLABS_SIZE_TOTAL:
|
|
|
|
return info->stats.total_size;
|
|
|
|
case PROCPS_SLABS_SIZE_ACTIVE:
|
|
|
|
return info->stats.active_size;
|
2015-07-13 10:30:00 +05:30
|
|
|
default:
|
2015-07-21 10:30:00 +05:30
|
|
|
return 0;
|
2015-07-04 10:29:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
PROCPS_EXPORT int procps_slabs_getstack (
|
2015-07-04 10:29:59 +05:30
|
|
|
struct procps_slabinfo *info,
|
2015-07-21 10:30:00 +05:30
|
|
|
struct slab_result *these)
|
2015-07-04 10:29:59 +05:30
|
|
|
{
|
2015-07-11 10:30:00 +05:30
|
|
|
if (info == NULL || these == NULL)
|
2015-07-04 10:29:59 +05:30
|
|
|
return -EINVAL;
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
for (;;) {
|
2015-07-11 10:30:00 +05:30
|
|
|
switch (these->item) {
|
|
|
|
case PROCPS_SLABS_OBJS:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->stats.nr_objs;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABS_AOBJS:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->stats.nr_active_objs;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABS_PAGES:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->stats.nr_pages;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABS_SLABS:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->stats.nr_slabs;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABS_ASLABS:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->stats.nr_active_slabs;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABS_CACHES:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->stats.nr_caches;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABS_ACACHES:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->stats.nr_active_caches;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABS_SIZE_AVG:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->stats.avg_obj_size;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABS_SIZE_MIN:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->stats.min_obj_size;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABS_SIZE_MAX:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->stats.max_obj_size;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABS_SIZE_TOTAL:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.ul_int = info->stats.total_size;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABS_SIZE_ACTIVE:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.ul_int = info->stats.active_size;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
2015-07-13 10:30:00 +05:30
|
|
|
case PROCPS_SLABS_noop:
|
2015-07-21 10:30:00 +05:30
|
|
|
// don't disturb potential user data in the result struct
|
2015-07-13 10:30:00 +05:30
|
|
|
break;
|
2015-07-21 10:30:00 +05:30
|
|
|
case PROCPS_SLABS_stack_end:
|
|
|
|
return 0;
|
2015-07-11 10:30:00 +05:30
|
|
|
default:
|
|
|
|
return -EINVAL;
|
2015-07-04 10:29:59 +05:30
|
|
|
}
|
2015-07-21 10:30:00 +05:30
|
|
|
++these;
|
|
|
|
}
|
2004-01-25 04:03:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-07-11 10:30:00 +05:30
|
|
|
* procps_slabnode_getname():
|
2015-07-04 10:29:59 +05:30
|
|
|
*
|
|
|
|
* @info: slabinfo structure with data read in
|
|
|
|
* @nodeid: number of node we want the name for
|
|
|
|
*
|
|
|
|
* Find the name of the given node
|
|
|
|
*
|
|
|
|
* Returns: name or NULL on error
|
|
|
|
*/
|
2015-07-11 10:30:00 +05:30
|
|
|
PROCPS_EXPORT const char *procps_slabnode_getname (
|
2015-07-04 10:29:59 +05:30
|
|
|
struct procps_slabinfo *info,
|
|
|
|
int nodeid)
|
|
|
|
{
|
|
|
|
if (info == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (nodeid > info->nodes_used)
|
|
|
|
return NULL;
|
|
|
|
return info->nodes[nodeid].name;
|
|
|
|
}
|
|
|
|
|
2015-07-11 10:30:00 +05:30
|
|
|
PROCPS_EXPORT unsigned long procps_slabnode_get (
|
2015-07-04 10:29:59 +05:30
|
|
|
struct procps_slabinfo *info,
|
2015-07-11 10:30:00 +05:30
|
|
|
enum slabnode_item item,
|
2015-07-04 10:29:59 +05:30
|
|
|
int nodeid)
|
|
|
|
{
|
2015-07-21 10:30:00 +05:30
|
|
|
/* note: most of the results we might return are actually just
|
|
|
|
unsigned int, but we must accommodate the largest potential
|
|
|
|
result and so return an unsigned long */
|
2015-07-11 10:30:00 +05:30
|
|
|
if (info == NULL)
|
2015-07-04 10:29:59 +05:30
|
|
|
return -EINVAL;
|
|
|
|
|
2015-07-11 10:30:00 +05:30
|
|
|
switch (item) {
|
2015-07-04 10:29:59 +05:30
|
|
|
case PROCPS_SLABNODE_SIZE:
|
2015-07-11 10:30:00 +05:30
|
|
|
return info->nodes[nodeid].cache_size;
|
2015-07-04 10:29:59 +05:30
|
|
|
case PROCPS_SLABNODE_OBJS:
|
2015-07-11 10:30:00 +05:30
|
|
|
return info->nodes[nodeid].nr_objs;
|
2015-07-04 10:29:59 +05:30
|
|
|
case PROCPS_SLABNODE_AOBJS:
|
2015-07-11 10:30:00 +05:30
|
|
|
return info->nodes[nodeid].nr_active_objs;
|
2015-07-04 10:29:59 +05:30
|
|
|
case PROCPS_SLABNODE_OBJ_SIZE:
|
2015-07-11 10:30:00 +05:30
|
|
|
return info->nodes[nodeid].obj_size;
|
2015-07-04 10:29:59 +05:30
|
|
|
case PROCPS_SLABNODE_OBJS_PER_SLAB:
|
2015-07-11 10:30:00 +05:30
|
|
|
return info->nodes[nodeid].objs_per_slab;
|
2015-07-04 10:29:59 +05:30
|
|
|
case PROCPS_SLABNODE_PAGES_PER_SLAB:
|
2015-07-11 10:30:00 +05:30
|
|
|
return info->nodes[nodeid].pages_per_slab;
|
2015-07-04 10:29:59 +05:30
|
|
|
case PROCPS_SLABNODE_SLABS:
|
2015-07-11 10:30:00 +05:30
|
|
|
return info->nodes[nodeid].nr_slabs;
|
2015-07-04 10:29:59 +05:30
|
|
|
case PROCPS_SLABNODE_ASLABS:
|
2015-07-11 10:30:00 +05:30
|
|
|
return info->nodes[nodeid].nr_active_slabs;
|
2015-07-04 10:29:59 +05:30
|
|
|
case PROCPS_SLABNODE_USE:
|
2015-07-11 10:30:00 +05:30
|
|
|
return info->nodes[nodeid].use;
|
2015-07-04 10:29:59 +05:30
|
|
|
default:
|
2015-07-21 10:30:00 +05:30
|
|
|
return 0;
|
2015-07-11 10:30:00 +05:30
|
|
|
}
|
2015-07-04 10:29:59 +05:30
|
|
|
}
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
PROCPS_EXPORT int procps_slabnode_getstack (
|
2015-07-11 10:30:00 +05:30
|
|
|
struct procps_slabinfo *info,
|
2015-07-21 10:30:00 +05:30
|
|
|
struct slab_result *these,
|
2015-07-11 10:30:00 +05:30
|
|
|
int nodeid)
|
2015-07-04 10:29:59 +05:30
|
|
|
{
|
2015-07-11 10:30:00 +05:30
|
|
|
if (info == NULL || these == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
if (nodeid > info->nodes_used)
|
|
|
|
return -EINVAL;
|
2015-07-04 10:29:59 +05:30
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
for (;;) {
|
2015-07-11 10:30:00 +05:30
|
|
|
switch (these->item) {
|
|
|
|
case PROCPS_SLABNODE_SIZE:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.ul_int = info->nodes[nodeid].cache_size;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABNODE_OBJS:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->nodes[nodeid].nr_objs;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABNODE_AOBJS:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->nodes[nodeid].nr_active_objs;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABNODE_OBJ_SIZE:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->nodes[nodeid].obj_size;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABNODE_OBJS_PER_SLAB:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->nodes[nodeid].objs_per_slab;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABNODE_PAGES_PER_SLAB:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->nodes[nodeid].pages_per_slab;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABNODE_SLABS:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->nodes[nodeid].nr_slabs;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABNODE_ASLABS:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->nodes[nodeid].nr_active_slabs;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABNODE_USE:
|
2015-07-21 10:30:00 +05:30
|
|
|
these->result.u_int = info->nodes[nodeid].use;
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
|
|
|
case PROCPS_SLABNODE_NAME:
|
|
|
|
these->result.str = info->nodes[nodeid].name;
|
|
|
|
break;
|
|
|
|
case PROCPS_SLABNODE_noop:
|
2015-07-21 10:30:00 +05:30
|
|
|
// don't disturb potential user data in the result struct
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
2015-07-21 10:30:00 +05:30
|
|
|
case PROCPS_SLABNODE_stack_end:
|
|
|
|
return 0;
|
2015-07-11 10:30:00 +05:30
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-07-21 10:30:00 +05:30
|
|
|
++these;
|
|
|
|
}
|
2015-07-04 10:29:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
PROCPS_EXPORT int procps_slabnode_stack_fill (
|
2015-07-11 10:30:00 +05:30
|
|
|
struct procps_slabinfo *info,
|
2015-07-21 10:30:00 +05:30
|
|
|
struct slabnode_stack *stack,
|
2015-07-11 10:30:00 +05:30
|
|
|
int nodeid)
|
2015-07-04 10:29:59 +05:30
|
|
|
{
|
2015-07-13 10:30:00 +05:30
|
|
|
int rc;
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
if (info == NULL || stack == NULL || stack->head == NULL)
|
2015-07-11 10:30:00 +05:30
|
|
|
return -EINVAL;
|
|
|
|
|
2015-07-13 10:30:00 +05:30
|
|
|
if ((rc = procps_slabinfo_read(info)) < 0)
|
|
|
|
return rc;
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
return procps_slabnode_getstack(info, stack->head, nodeid);
|
2015-07-04 10:29:59 +05:30
|
|
|
}
|
|
|
|
|
2015-07-11 10:30:00 +05:30
|
|
|
/*
|
|
|
|
* procps_slabnode_count():
|
|
|
|
*
|
|
|
|
* @info: read in slabinfo structure
|
|
|
|
*
|
|
|
|
* Returns: number of nodes in @info or <0 on error
|
|
|
|
*/
|
|
|
|
PROCPS_EXPORT int procps_slabnode_count (
|
2015-07-21 10:30:00 +05:30
|
|
|
struct procps_slabinfo *info)
|
2015-07-04 10:29:59 +05:30
|
|
|
{
|
2015-07-21 10:30:00 +05:30
|
|
|
int rc = 0;
|
|
|
|
|
2015-07-11 10:30:00 +05:30
|
|
|
if (!info)
|
|
|
|
return -EINVAL;
|
2015-07-21 10:30:00 +05:30
|
|
|
if (!info->nodes_used)
|
|
|
|
rc = procps_slabinfo_read(info);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
2015-07-11 10:30:00 +05:30
|
|
|
return info->nodes_used;
|
2015-07-04 10:29:59 +05:30
|
|
|
}
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
PROCPS_EXPORT int procps_slabnode_stacks_fill (
|
2015-07-11 10:30:00 +05:30
|
|
|
struct procps_slabinfo *info,
|
2015-07-21 10:30:00 +05:30
|
|
|
struct slabnode_stack **stacks,
|
|
|
|
int maxstacks)
|
2015-07-04 10:29:59 +05:30
|
|
|
{
|
2015-07-11 10:30:00 +05:30
|
|
|
int i, rc;
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
if (info == NULL || *stacks == NULL)
|
2015-07-11 10:30:00 +05:30
|
|
|
return -EINVAL;
|
2015-07-21 10:30:00 +05:30
|
|
|
if (maxstacks < 1)
|
2015-07-11 10:30:00 +05:30
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if ((rc = procps_slabinfo_read(info)) < 0)
|
|
|
|
return rc;
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
if (maxstacks > info->stacked->depth)
|
|
|
|
maxstacks = info->stacked->depth;
|
|
|
|
if (maxstacks > info->nodes_used)
|
|
|
|
maxstacks = info->nodes_used;
|
2015-07-11 10:30:00 +05:30
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
for (i = 0; i < maxstacks; i++) {
|
|
|
|
if (stacks[i] == NULL)
|
2015-07-11 10:30:00 +05:30
|
|
|
break;
|
2015-07-21 10:30:00 +05:30
|
|
|
if ((rc = procps_slabnode_getstack(info, stacks[i]->head, i) < 0))
|
2015-07-11 10:30:00 +05:30
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
info->stacked->inuse = i;
|
|
|
|
return info->stacked->inuse;
|
2015-07-04 10:29:59 +05:30
|
|
|
}
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
static void stacks_validate (struct slabnode_stack **v, const char *who)
|
2015-07-04 10:29:59 +05:30
|
|
|
{
|
2015-07-11 10:30:00 +05:30
|
|
|
#if 0
|
|
|
|
#include <stdio.h>
|
2015-07-21 10:30:00 +05:30
|
|
|
int i, t, x, n = 0;
|
|
|
|
struct stack_vectors *p = (struct stack_vectors *)v - 1;
|
2015-07-11 10:30:00 +05:30
|
|
|
|
|
|
|
fprintf(stderr, "%s: called by '%s'\n", __func__, who);
|
|
|
|
fprintf(stderr, "%s: owned by %p (whose self = %p)\n", __func__, p->owner, p->owner->self);
|
|
|
|
for (x = 0; v[x]; x++) {
|
2015-07-21 10:30:00 +05:30
|
|
|
struct slabnode_stack *h = v[x];
|
|
|
|
struct slab_result *r = h->head;
|
2015-07-11 10:30:00 +05:30
|
|
|
fprintf(stderr, "%s: vector[%02d] = %p", __func__, x, h);
|
2015-07-21 10:30:00 +05:30
|
|
|
for (i = 0; r->item < PROCPS_SLABNODE_stack_end; i++, r++)
|
|
|
|
;
|
|
|
|
t = i + 1;
|
|
|
|
fprintf(stderr, ", stack %d found %d elements\n", n, i);
|
2015-07-11 10:30:00 +05:30
|
|
|
++n;
|
|
|
|
}
|
2015-07-21 10:30:00 +05:30
|
|
|
fprintf(stderr, "%s: found %d stack(s), each %d bytes (including eos)\n", __func__, x, (int)sizeof(struct slab_result) * t);
|
|
|
|
fprintf(stderr, "%s: found %d stack(s)\n", __func__, x);
|
|
|
|
fprintf(stderr, "%s: sizeof(struct slabnode_stack) = %2d\n", __func__, (int)sizeof(struct slabnode_stack));
|
2015-07-22 10:30:00 +05:30
|
|
|
fprintf(stderr, "%s: sizeof(struct slab_result) = %2d\n", __func__, (int)sizeof(struct slab_result));
|
2015-07-11 10:30:00 +05:30
|
|
|
fputc('\n', stderr);
|
|
|
|
return;
|
|
|
|
#endif
|
2015-07-04 10:29:59 +05:30
|
|
|
}
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
static struct slab_result *stack_make (
|
|
|
|
struct slab_result *p,
|
2015-07-11 10:30:00 +05:30
|
|
|
int maxitems,
|
|
|
|
enum slabnode_item *items)
|
2015-07-04 10:29:59 +05:30
|
|
|
{
|
2015-07-21 10:30:00 +05:30
|
|
|
struct slab_result *p_sav = p;
|
2015-07-11 10:30:00 +05:30
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < maxitems; i++) {
|
2015-07-21 10:30:00 +05:30
|
|
|
p->item = items[i];
|
|
|
|
// note: we rely on calloc to initialize actual result
|
2015-07-11 10:30:00 +05:30
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p_sav;
|
2015-07-04 10:29:59 +05:30
|
|
|
}
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
static int stack_items_valid (
|
|
|
|
int maxitems,
|
|
|
|
enum slabnode_item *items)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < maxitems; i++) {
|
2015-07-23 10:30:00 +05:30
|
|
|
if (items[i] < 0)
|
2015-07-21 10:30:00 +05:30
|
|
|
return 0;
|
|
|
|
if (items[i] > PROCPS_SLABNODE_stack_end)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (items[maxitems -1] != PROCPS_SLABNODE_stack_end)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-07-11 10:30:00 +05:30
|
|
|
/*
|
2015-07-21 10:30:00 +05:30
|
|
|
* procps_slabnode_stacks_alloc():
|
2015-07-11 10:30:00 +05:30
|
|
|
*
|
2015-07-21 10:30:00 +05:30
|
|
|
* Allocate and initialize one or more stacks each of which is anchored in an
|
|
|
|
* associated slabnode_stack structure (which may include extra user space).
|
2015-07-11 10:30:00 +05:30
|
|
|
*
|
2015-07-21 10:30:00 +05:30
|
|
|
* All such stacks will will have their result structures properly primed with
|
|
|
|
* 'items', while the result itself will be zeroed.
|
2015-07-11 10:30:00 +05:30
|
|
|
*
|
2015-07-21 10:30:00 +05:30
|
|
|
* Returns an array of pointers representing the 'heads' of each new stack.
|
2015-07-11 10:30:00 +05:30
|
|
|
*/
|
2015-07-21 10:30:00 +05:30
|
|
|
PROCPS_EXPORT struct slabnode_stack **procps_slabnode_stacks_alloc (
|
2015-07-11 10:30:00 +05:30
|
|
|
struct procps_slabinfo *info,
|
2015-07-21 10:30:00 +05:30
|
|
|
int maxstacks,
|
2015-07-11 10:30:00 +05:30
|
|
|
int maxitems,
|
|
|
|
enum slabnode_item *items)
|
2015-07-04 10:29:59 +05:30
|
|
|
{
|
2015-07-21 10:30:00 +05:30
|
|
|
struct stacks_anchor *p_blob;
|
|
|
|
struct stack_vectors *p_vect;
|
|
|
|
struct slabnode_stack *p_head;
|
2015-07-11 10:30:00 +05:30
|
|
|
size_t vect_size, head_size, list_size, blob_size;
|
|
|
|
void *v_head, *v_list;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (info == NULL || items == NULL)
|
|
|
|
return NULL;
|
2015-07-21 10:30:00 +05:30
|
|
|
if (maxstacks < 1 || maxitems < 1)
|
|
|
|
return NULL;
|
|
|
|
if (!stack_items_valid(maxitems, items))
|
2015-07-11 10:30:00 +05:30
|
|
|
return NULL;
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
vect_size = sizeof(struct stack_vectors); // address vector struct
|
|
|
|
vect_size += sizeof(void *) * maxstacks; // plus vectors themselves
|
2015-07-11 10:30:00 +05:30
|
|
|
vect_size += sizeof(void *); // plus NULL delimiter
|
2015-07-22 10:30:00 +05:30
|
|
|
head_size = sizeof(struct slabnode_stack); // a head struct
|
2015-07-21 10:30:00 +05:30
|
|
|
list_size = sizeof(struct slab_result) * maxitems; // a results stack
|
|
|
|
blob_size = sizeof(struct stacks_anchor); // the anchor itself
|
2015-07-11 10:30:00 +05:30
|
|
|
blob_size += vect_size; // all vectors + delims
|
2015-07-23 10:30:00 +05:30
|
|
|
blob_size += head_size * maxstacks; // all head structs
|
2015-07-21 10:30:00 +05:30
|
|
|
blob_size += list_size * maxstacks; // all results stacks
|
2015-07-11 10:30:00 +05:30
|
|
|
|
|
|
|
/* note: all memory is allocated in a single blob, facilitating a later free().
|
|
|
|
as a minimum, it's important that the result structures themselves always be
|
2015-07-21 10:30:00 +05:30
|
|
|
contiguous for any given stack (just as they are when defined statically). */
|
2015-07-11 10:30:00 +05:30
|
|
|
if (NULL == (p_blob = calloc(1, blob_size)))
|
|
|
|
return NULL;
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
p_blob->next = info->stacked;
|
|
|
|
info->stacked = p_blob;
|
2015-07-11 10:30:00 +05:30
|
|
|
p_blob->self = p_blob;
|
2015-07-21 10:30:00 +05:30
|
|
|
p_blob->vectors = (void *)p_blob + sizeof(struct stacks_anchor);
|
2015-07-11 10:30:00 +05:30
|
|
|
p_vect = p_blob->vectors;
|
|
|
|
p_vect->owner = p_blob->self;
|
2015-07-21 10:30:00 +05:30
|
|
|
p_vect->heads = (void *)p_vect + sizeof(struct stack_vectors);
|
2015-07-11 10:30:00 +05:30
|
|
|
v_head = (void *)p_vect + vect_size;
|
2015-07-21 10:30:00 +05:30
|
|
|
v_list = v_head + (head_size * maxstacks);
|
2015-07-11 10:30:00 +05:30
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
for (i = 0; i < maxstacks; i++) {
|
|
|
|
p_head = (struct slabnode_stack *)v_head;
|
|
|
|
p_head->head = stack_make((struct slab_result *)v_list, maxitems, items);
|
2015-07-11 10:30:00 +05:30
|
|
|
p_blob->vectors->heads[i] = p_head;
|
|
|
|
v_list += list_size;
|
|
|
|
v_head += head_size;
|
|
|
|
}
|
2015-07-21 10:30:00 +05:30
|
|
|
p_blob->depth = maxstacks;
|
|
|
|
stacks_validate(p_blob->vectors->heads, __func__);
|
2015-07-11 10:30:00 +05:30
|
|
|
return p_blob->vectors->heads;
|
2015-07-04 10:29:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-07-21 10:30:00 +05:30
|
|
|
* procps_slabnode_stack_alloc():
|
2015-07-04 10:29:59 +05:30
|
|
|
*
|
2015-07-21 10:30:00 +05:30
|
|
|
* Allocate and initialize a single result stack under a simplified interface.
|
2015-07-04 10:29:59 +05:30
|
|
|
*
|
2015-07-21 10:30:00 +05:30
|
|
|
* Such a stack will will have its result structures properly primed with
|
|
|
|
* 'items', while the result itself will be zeroed.
|
2015-07-04 10:29:59 +05:30
|
|
|
*
|
|
|
|
*/
|
2015-07-21 10:30:00 +05:30
|
|
|
PROCPS_EXPORT struct slabnode_stack *procps_slabnode_stack_alloc (
|
2015-07-04 10:29:59 +05:30
|
|
|
struct procps_slabinfo *info,
|
2015-07-11 10:30:00 +05:30
|
|
|
int maxitems,
|
|
|
|
enum slabnode_item *items)
|
2015-07-04 10:29:59 +05:30
|
|
|
{
|
2015-07-21 10:30:00 +05:30
|
|
|
struct slabnode_stack **v;
|
2015-07-04 10:29:59 +05:30
|
|
|
|
2015-07-11 10:30:00 +05:30
|
|
|
if (info == NULL || items == NULL || maxitems < 1)
|
|
|
|
return NULL;
|
2015-07-22 10:30:00 +05:30
|
|
|
v = procps_slabnode_stacks_alloc(info, 1, maxitems, items);
|
2015-07-11 10:30:00 +05:30
|
|
|
if (!v)
|
|
|
|
return NULL;
|
2015-07-21 10:30:00 +05:30
|
|
|
stacks_validate(v, __func__);
|
2015-07-11 10:30:00 +05:30
|
|
|
return v[0];
|
|
|
|
}
|
2015-07-04 10:29:59 +05:30
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
static int stacks_sort (
|
|
|
|
const struct slabnode_stack **A,
|
|
|
|
const struct slabnode_stack **B,
|
2015-07-11 10:30:00 +05:30
|
|
|
enum slabnode_item *offset)
|
|
|
|
{
|
2015-07-21 10:30:00 +05:30
|
|
|
const struct slab_result *a = (*A)->head + *offset;
|
|
|
|
const struct slab_result *b = (*B)->head + *offset;
|
2015-07-13 10:30:00 +05:30
|
|
|
// note: everything will be sorted high-to-low
|
2015-07-21 10:30:00 +05:30
|
|
|
switch (a->item) {
|
|
|
|
case PROCPS_SLABNODE_noop:
|
|
|
|
case PROCPS_SLABNODE_stack_end:
|
|
|
|
break;
|
|
|
|
case PROCPS_SLABNODE_NAME:
|
|
|
|
return strcoll(a->result.str, b->result.str);
|
|
|
|
case PROCPS_SLABNODE_SIZE:
|
|
|
|
if ( a->result.ul_int > b->result.ul_int ) return -1;
|
|
|
|
if ( a->result.ul_int < b->result.ul_int ) return +1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ( a->result.u_int > b->result.u_int ) return -1;
|
|
|
|
if ( a->result.u_int < b->result.u_int ) return +1;
|
|
|
|
break;
|
|
|
|
}
|
2015-07-04 10:29:59 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-07-21 10:30:00 +05:30
|
|
|
* procps_slabnode_stacks_sort():
|
2015-07-04 10:29:59 +05:30
|
|
|
*
|
2015-07-21 10:30:00 +05:30
|
|
|
* Sort stacks anchored as 'heads' in the passed slabnode_stack pointers
|
2015-07-11 10:30:00 +05:30
|
|
|
* array based on the designated sort enumerator.
|
2004-01-25 04:03:56 +05:30
|
|
|
*
|
2015-07-13 10:30:00 +05:30
|
|
|
* Returns those same addresses sorted.
|
2015-07-11 10:30:00 +05:30
|
|
|
*
|
2015-07-21 10:30:00 +05:30
|
|
|
* Note: all of the stacks must be homogeneous (of equal length and content).
|
2004-01-25 04:03:56 +05:30
|
|
|
*/
|
2015-07-21 10:30:00 +05:30
|
|
|
PROCPS_EXPORT struct slabnode_stack **procps_slabnode_stacks_sort (
|
2015-07-11 10:30:00 +05:30
|
|
|
struct procps_slabinfo *info,
|
2015-07-21 10:30:00 +05:30
|
|
|
struct slabnode_stack **stacks,
|
|
|
|
int numstacked,
|
2015-07-11 10:30:00 +05:30
|
|
|
enum slabnode_item sort)
|
2004-01-25 04:03:56 +05:30
|
|
|
{
|
2015-07-11 10:30:00 +05:30
|
|
|
#define QSORT_r int (*)(const void *, const void *, void *)
|
2016-05-14 18:02:35 +05:30
|
|
|
struct slab_result *p;
|
2015-07-11 10:30:00 +05:30
|
|
|
int offset = 0;;
|
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
if (info == NULL || stacks == NULL)
|
2015-07-11 10:30:00 +05:30
|
|
|
return NULL;
|
2016-05-14 18:02:35 +05:30
|
|
|
|
2015-07-13 10:30:00 +05:30
|
|
|
if (sort < 0 || sort > PROCPS_SLABNODE_noop)
|
2015-07-11 10:30:00 +05:30
|
|
|
return NULL;
|
2015-07-21 10:30:00 +05:30
|
|
|
if (numstacked > info->stacked->depth)
|
2015-07-11 10:30:00 +05:30
|
|
|
return NULL;
|
2015-07-21 10:30:00 +05:30
|
|
|
if (numstacked < 2)
|
|
|
|
return stacks;
|
2015-07-11 10:30:00 +05:30
|
|
|
|
2015-07-21 10:30:00 +05:30
|
|
|
if (numstacked > info->stacked->inuse)
|
|
|
|
numstacked = info->stacked->inuse;
|
2015-07-11 10:30:00 +05:30
|
|
|
|
2016-05-14 18:02:35 +05:30
|
|
|
p = stacks[0]->head;
|
2015-07-11 10:30:00 +05:30
|
|
|
for (;;) {
|
|
|
|
if (p->item == sort)
|
|
|
|
break;
|
|
|
|
++offset;
|
2015-07-21 10:30:00 +05:30
|
|
|
if (p->item == PROCPS_SLABNODE_stack_end)
|
2015-07-11 10:30:00 +05:30
|
|
|
return NULL;
|
2015-07-21 10:30:00 +05:30
|
|
|
++p;
|
2015-07-11 10:30:00 +05:30
|
|
|
}
|
2015-07-21 10:30:00 +05:30
|
|
|
qsort_r(stacks, numstacked, sizeof(void *), (QSORT_r)stacks_sort, &offset);
|
|
|
|
return stacks;
|
2015-07-11 10:30:00 +05:30
|
|
|
#undef QSORT_r
|
2004-01-25 04:03:56 +05:30
|
|
|
}
|