"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/core-setting.c" (15 Mar 2019, 9221 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "core-setting.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) 2017-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 #include "stress-ng.h"
   26 
   27 static setting_t *setting_head; /* setting list head */
   28 static setting_t *setting_tail; /* setting list tail */
   29 
   30 #if defined(DEBUG_SETTINGS)
   31 #define DBG(...)    pr_inf(__VA_ARGS__)
   32 #else
   33 #define DBG(...)
   34 #endif
   35 
   36 /*
   37  *  free_settings()
   38  *  free the saved settings
   39  */
   40 void free_settings(void)
   41 {
   42     setting_t *setting = setting_head;
   43 
   44     while (setting) {
   45         setting_t *next = setting->next;
   46 
   47         free(setting->name);
   48         free(setting);
   49         setting = next;
   50     }
   51     setting_head = NULL;
   52     setting_tail = NULL;
   53 }
   54 
   55 
   56 /*
   57  *  set_setting_generic()
   58  *  set a new setting;
   59  */
   60 static int set_setting_generic(
   61     const char *name,
   62     const type_id_t type_id,
   63     const void *value,
   64     const bool global)
   65 {
   66     setting_t *setting;
   67 
   68     if (!value) {
   69         (void)fprintf(stderr, "invalid setting '%s' value address (null)\n", name);
   70         _exit(EXIT_NOT_SUCCESS);
   71     }
   72     setting = calloc(1, sizeof *setting);
   73     if (!setting)
   74         goto err;
   75 
   76     setting->name = strdup(name);
   77     setting->proc = proc_current;
   78     setting->type_id = type_id;
   79     setting->global = global;
   80     if (!setting->name) {
   81         free(setting);
   82         goto err;
   83     }
   84 
   85     DBG("%s: %s, global = %s\n", __func__, name, global ? "true" : "false");
   86 
   87     switch (type_id) {
   88     case TYPE_ID_UINT8:
   89         setting->u.uint8 = *(const uint8_t *)value;
   90         DBG("%s: UINT8: %s -> %" PRIu8 "\n", __func__, name, setting->u.uint8);
   91         break;
   92     case TYPE_ID_INT8:
   93         setting->u.int8 = *(const int8_t *)value;
   94         DBG("%s: INT8: %s -> %" PRId8 "\n", __func__, name, setting->u.int8);
   95         break;
   96     case TYPE_ID_UINT16:
   97         setting->u.uint16 = *(const uint16_t *)value;
   98         DBG("%s: UINT16: %s -> %" PRIu16 "\n", __func__, name, setting->u.uint16);
   99         break;
  100     case TYPE_ID_INT16:
  101         setting->u.int16 = *(const int16_t *)value;
  102         DBG("%s: INT16: %s -> %" PRId16 "\n", __func__, name, setting->u.int16);
  103         break;
  104     case TYPE_ID_UINT32:
  105         setting->u.uint32 = *(const uint32_t *)value;
  106         DBG("%s: UINT32: %s -> %" PRIu32 "\n", __func__, name, setting->u.uint32);
  107         break;
  108     case TYPE_ID_INT32:
  109         setting->u.int32 = *(const int32_t *)value;
  110         DBG("%s: INT32: %s -> %" PRId32 "\n", __func__, name, setting->u.int32);
  111         break;
  112     case TYPE_ID_UINT64:
  113         setting->u.uint64 = *(const uint64_t *)value;
  114         DBG("%s: UINT64: %s -> %" PRIu64 "\n", __func__, name, setting->u.uint64);
  115         break;
  116     case TYPE_ID_INT64:
  117         setting->u.int64 = *(const int64_t *)value;
  118         DBG("%s: INT64: %s -> %" PRId64 "\n", __func__, name, setting->u.int64);
  119         break;
  120     case TYPE_ID_SIZE_T:
  121         setting->u.size = *(const size_t *)value;
  122         DBG("%s: SIZE_T: %s -> %zu\n", __func__, name, setting->u.size);
  123         break;
  124     case TYPE_ID_SSIZE_T:
  125         setting->u.ssize = *(const ssize_t *)value;
  126         DBG("%s: SSIZE_T: %s -> %zd\n", __func__, name, setting->u.ssize);
  127         break;
  128     case TYPE_ID_UINT:
  129         setting->u.uint = *(const unsigned int *)value;
  130         DBG("%s: UINT: %s -> %u\n", __func__, name, setting->u.uint);
  131         break;
  132     case TYPE_ID_INT:
  133         setting->u.sint = *(const int *)value;
  134         DBG("%s: UINT: %s -> %d\n", __func__, name, setting->u.sint);
  135         break;
  136     case TYPE_ID_ULONG:
  137         setting->u.ulong = *(const unsigned long  *)value;
  138         DBG("%s: ULONG: %s -> %lu\n", __func__, name, setting->u.ulong);
  139         break;
  140     case TYPE_ID_LONG:
  141         setting->u.slong = *(const long  *)value;
  142         DBG("%s: LONG: %s -> %ld\n", __func__, name, setting->u.slong);
  143         break;
  144     case TYPE_ID_OFF_T:
  145         setting->u.off = *(const long *)value;
  146         DBG("%s: OFF_T: %s -> %lu\n", __func__, name, (unsigned long)setting->u.off);
  147         break;
  148     case TYPE_ID_STR:
  149         setting->u.str = (const char *)value;
  150         DBG("%s: STR: %s -> %s\n", __func__, name, setting->u.str);
  151         break;
  152     case TYPE_ID_BOOL:
  153         setting->u.boolean = *(const bool *)value;
  154         DBG("%s: BOOL: %s -> %d\n", __func__, name, setting->u.boolean);
  155         break;
  156     case TYPE_ID_UINTPTR_T:
  157         setting->u.uintptr = *(const uintptr_t *)value;
  158         DBG("%s: UINTPTR_R: %s -> %p\n", __func__, name, (void *)setting->u.uintptr);
  159         break;
  160     case TYPE_ID_UNDEFINED:
  161     default:
  162         DBG("%s: UNDEF: %s -> ?\n", __func__, name);
  163         break;
  164     }
  165 
  166     if (setting_tail) {
  167         setting_tail->next = setting;
  168     } else {
  169         setting_head = setting;
  170     }
  171     setting_tail = setting;
  172 
  173     return 0;
  174 err:
  175     (void)fprintf(stderr, "cannot allocate setting '%s'\n", name);
  176     _exit(EXIT_NO_RESOURCE);
  177 
  178     return 1;
  179 }
  180 
  181 /*
  182  *  set_setting()
  183  *  set a new setting;
  184  */
  185 int set_setting(
  186     const char *name,
  187     const type_id_t type_id,
  188     const void *value)
  189 {
  190     return set_setting_generic(name, type_id, value, false);
  191 }
  192 
  193 /*
  194  *  set_setting_global()
  195  *  set a new global setting;
  196  */
  197 int set_setting_global(
  198     const char *name,
  199     const type_id_t type_id,
  200     const void *value)
  201 {
  202     return set_setting_generic(name, type_id, value, true);
  203 }
  204 
  205 
  206 /*
  207  *  get_setting()
  208  *  get an existing setting;
  209  */
  210 bool get_setting(const char *name, void *value)
  211 {
  212     setting_t *setting;
  213     bool set = false;
  214     bool found = false;
  215 
  216     DBG("%s: get %s\n", __func__, name);
  217 
  218     for (setting = setting_head; setting; setting = setting->next) {
  219         if (setting->proc == proc_current)
  220             found = true;
  221         if (found && ((setting->proc != proc_current) && (!setting->global)))
  222             break;
  223 
  224         if (!strcmp(setting->name, name)) {
  225             switch (setting->type_id) {
  226             case TYPE_ID_UINT8:
  227                 set = true;
  228                 *(uint8_t *)value = setting->u.uint8;
  229                 DBG("%s: UINT8: %s -> %" PRIu8 "\n", __func__, name, setting->u.uint8);
  230                 break;
  231             case TYPE_ID_INT8:
  232                 set = true;
  233                 *(int8_t *)value = setting->u.int8;
  234                 DBG("%s: INT8: %s -> %" PRId8 "\n", __func__, name, setting->u.int8);
  235                 break;
  236             case TYPE_ID_UINT16:
  237                 set = true;
  238                 *(uint16_t *)value = setting->u.uint16;
  239                 DBG("%s: UINT16: %s -> %" PRIu16 "\n", __func__, name, setting->u.uint16);
  240                 break;
  241             case TYPE_ID_INT16:
  242                 set = true;
  243                 *(int16_t *)value = setting->u.int16;
  244                 DBG("%s: INT16: %s -> %" PRId16 "\n", __func__, name, setting->u.int16);
  245                 break;
  246             case TYPE_ID_UINT32:
  247                 set = true;
  248                 *(uint32_t *)value = setting->u.uint32;
  249                 DBG("%s: UINT32: %s -> %" PRIu32 "\n", __func__, name, setting->u.uint32);
  250                 break;
  251             case TYPE_ID_INT32:
  252                 set = true;
  253                 *(int32_t *)value = setting->u.int32;
  254                 DBG("%s: INT32: %s -> %" PRId32 "\n", __func__, name, setting->u.int32);
  255                 break;
  256             case TYPE_ID_UINT64:
  257                 set = true;
  258                 *(uint64_t *)value = setting->u.uint64;
  259                 DBG("%s: UINT64: %s -> %" PRIu64 "\n", __func__, name, setting->u.uint64);
  260                 break;
  261             case TYPE_ID_INT64:
  262                 set = true;
  263                 *(int64_t *)value = setting->u.int64;
  264                 DBG("%s: INT64: %s -> %" PRId64 "\n", __func__, name, setting->u.int64);
  265                 break;
  266             case TYPE_ID_SIZE_T:
  267                 set = true;
  268                 *(size_t *)value = setting->u.size;
  269                 DBG("%s: SIZE_T: %s -> %zu\n", __func__, name, setting->u.size);
  270                 break;
  271             case TYPE_ID_SSIZE_T:
  272                 set = true;
  273                 *(ssize_t *)value = setting->u.ssize;
  274                 DBG("%s: SSIZE_T: %s -> %zd\n", __func__, name, setting->u.ssize);
  275                 break;
  276             case TYPE_ID_UINT:
  277                 set = true;
  278                 *(unsigned int *)value = setting->u.uint;
  279                 DBG("%s: UINT: %s -> %u\n", __func__, name, setting->u.uint);
  280                 break;
  281             case TYPE_ID_INT:
  282                 set = true;
  283                 *(int *)value = setting->u.sint;
  284                 DBG("%s: UINT: %s -> %d\n", __func__, name, setting->u.sint);
  285                 break;
  286             case TYPE_ID_ULONG:
  287                 set = true;
  288                 *(unsigned long  *)value = setting->u.ulong;
  289                 DBG("%s: ULONG: %s -> %lu\n", __func__, name, setting->u.ulong);
  290                 break;
  291             case TYPE_ID_LONG:
  292                 set = true;
  293                 *(long *)value = setting->u.slong;
  294                 DBG("%s: LONG: %s -> %ld\n", __func__, name, setting->u.slong);
  295                 break;
  296             case TYPE_ID_OFF_T:
  297                 set = true;
  298                 *(long  *)value = setting->u.off;
  299                 DBG("%s: OFF_T: %s -> %lu\n", __func__, name, (unsigned long)setting->u.off);
  300                 break;
  301             case TYPE_ID_STR:
  302                 set = true;
  303                 *(const char **)value = setting->u.str;
  304                 DBG("%s: STR: %s -> %s\n", __func__, name, setting->u.str);
  305                 break;
  306             case TYPE_ID_BOOL:
  307                 set = true;
  308                 *(bool *)value = setting->u.boolean;
  309                 DBG("%s: BOOL: %s -> %d\n", __func__, name, setting->u.boolean);
  310                 break;
  311             case TYPE_ID_UINTPTR_T:
  312                 set = true;
  313                 *(uintptr_t *)value = setting->u.uintptr;
  314                 DBG("%s: UINTPTR_T: %s -> %p\n", __func__, name, (void *)setting->u.uintptr);
  315                 break;
  316             case TYPE_ID_UNDEFINED:
  317             default:
  318                 set = true;
  319                 DBG("%s: UNDEF: %s -> ?\n", __func__, name);
  320                 break;
  321             }
  322         }
  323     }
  324     return set;
  325 }