From 65ac30359e49ee717397e39950d7c24a6610d57c Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Fri, 22 Sep 2017 08:33:13 +0200 Subject: [PATCH] Add function to remap a structure This function will be used to map two structures from two different sysstat versions. It will thus be possible to add new fields, e.g., in structures containing statistics and still be possible for another sysstat version to read its contents properly. Signed-off-by: Sebastien GODARD --- sa.h | 2 ++ sa_common.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/sa.h b/sa.h index a79c48e..50f4c45 100644 --- a/sa.h +++ b/sa.h @@ -1194,6 +1194,8 @@ __nr_t read_vol_act_structures (int, struct activity * [], char *, struct file_magic *, unsigned int, int, int); int reallocate_vol_act_structures (struct activity * [], unsigned int, unsigned int); +void remap_struct + (int [], int [], void *, int); void replace_nonprintable_char (int, char *); int sa_fread diff --git a/sa_common.c b/sa_common.c index d7cb66e..2fe29f3 100644 --- a/sa_common.c +++ b/sa_common.c @@ -1029,6 +1029,77 @@ void swap_struct(int types_nr[], void *ps, int is64bit) } } +/* + *************************************************************************** + * Map the fields of a structure containing statistics read from a file to + * those of the structure known by current sysstat version. + * Each structure (either read from file or from current sysstat version) + * are described by 3 values: The number of [unsigned] long long integers, + * the number of [unsigned] long integers following in the structure, and + * last the number of [unsigned] integers. + * We assume that those numbers will *never* decrease with newer sysstat + * versions. + * + * IN: + * @gtypes_nr Structure description as expected for current sysstat version. + * @ftypes_nr Structure description as read from file. + * @ps Pointer on structure containing statistics. + * @st_size Size of the structure containing statistics. This is the size + * of the structure *read from file* (not the size of the + * structure expected by current sysstat version). + *************************************************************************** + */ +void remap_struct(int gtypes_nr[], int ftypes_nr[], void *ps, int st_size) +{ + int d; + + /* Remap [unsigned] long fields */ + d = gtypes_nr[0] - ftypes_nr[0]; + if (d) { + memmove(((char *) ps) + gtypes_nr[0] * ULL_ALIGNMENT_WIDTH, + ((char *) ps) + ftypes_nr[0] * ULL_ALIGNMENT_WIDTH, + st_size - ftypes_nr[0] * ULL_ALIGNMENT_WIDTH); + if (d > 0) { + memset(((char *) ps) + ftypes_nr[0] * ULL_ALIGNMENT_WIDTH, + 0, d * ULL_ALIGNMENT_WIDTH); + } + } + /* Remap [unsigned] int fields */ + d = gtypes_nr[1] - ftypes_nr[1]; + if (d) { + memmove(((char *) ps) + gtypes_nr[0] * ULL_ALIGNMENT_WIDTH + + gtypes_nr[1] * UL_ALIGNMENT_WIDTH, + ((char *) ps) + gtypes_nr[0] * ULL_ALIGNMENT_WIDTH + + ftypes_nr[1] * UL_ALIGNMENT_WIDTH, + st_size - ftypes_nr[0] * ULL_ALIGNMENT_WIDTH + - ftypes_nr[1] * UL_ALIGNMENT_WIDTH); + if (d > 0) { + memset(((char *) ps) + gtypes_nr[0] * ULL_ALIGNMENT_WIDTH + + ftypes_nr[1] * UL_ALIGNMENT_WIDTH, + 0, d * UL_ALIGNMENT_WIDTH); + } + } + /* Remap possible fields (like strings of chars) following int fields */ + d = gtypes_nr[2] - ftypes_nr[2]; + if (d) { + memmove(((char *) ps) + gtypes_nr[0] * ULL_ALIGNMENT_WIDTH + + gtypes_nr[1] * UL_ALIGNMENT_WIDTH + + gtypes_nr[2] * U_ALIGNMENT_WIDTH, + ((char *) ps) + gtypes_nr[0] * ULL_ALIGNMENT_WIDTH + + gtypes_nr[1] * UL_ALIGNMENT_WIDTH + + ftypes_nr[2] * U_ALIGNMENT_WIDTH, + st_size - ftypes_nr[0] * ULL_ALIGNMENT_WIDTH + - ftypes_nr[1] * UL_ALIGNMENT_WIDTH + - ftypes_nr[2] * U_ALIGNMENT_WIDTH); + if (d > 0) { + memset(((char *) ps) + gtypes_nr[0] * ULL_ALIGNMENT_WIDTH + + gtypes_nr[1] * UL_ALIGNMENT_WIDTH + + ftypes_nr[2] * U_ALIGNMENT_WIDTH, + 0, d * U_ALIGNMENT_WIDTH); + } + } +} + /* *************************************************************************** * Read data from a system activity data file. -- 2.50.1