"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/core-io-priority.c" (15 Mar 2019, 3051 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-io-priority.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) 2013-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 /*
   28  *  get_opt_ionice_class()
   29  *  string io scheduler to IOPRIO_CLASS
   30  */
   31 int32_t get_opt_ionice_class(const char *const str)
   32 {
   33 #if defined(IOPRIO_CLASS_IDLE)
   34     if (!strcmp("idle", str))
   35         return IOPRIO_CLASS_IDLE;
   36 #endif
   37 #if defined(IOPRIO_CLASS_BE)
   38     if (!strcmp("besteffort", str) ||
   39         !strcmp("be", str))
   40         return IOPRIO_CLASS_BE;
   41 #endif
   42 #if defined(IOPRIO_CLASS_RT)
   43     if (!strcmp("realtime", str) ||
   44         !strcmp("rt", str))
   45         return IOPRIO_CLASS_RT;
   46 #endif
   47     if (strcmp("which", str))
   48         (void)fprintf(stderr, "Invalid ionice-class option: %s\n", str);
   49 
   50     (void)fprintf(stderr, "Available options are:");
   51 #if defined(IOPRIO_CLASS_IDLE)
   52     (void)fprintf(stderr, " idle");
   53 #endif
   54 #if defined(IOPRIO_CLASS_BE)
   55     (void)fprintf(stderr, " besteffort be");
   56 #endif
   57 #if defined(IOPRIO_CLASS_RT)
   58     (void)fprintf(stderr, " realtime rt");
   59 #endif
   60     (void)fprintf(stderr, "\n");
   61 
   62     _exit(EXIT_FAILURE);
   63 }
   64 
   65 #if defined(__NR_ioprio_set)
   66 /*
   67  *  set_iopriority()
   68  *  check ioprio settings and set
   69  */
   70 void set_iopriority(const int32_t class, const int32_t level)
   71 {
   72     int data = level, rc;
   73 
   74     switch (class) {
   75     case UNDEFINED: /* No preference, don't set */
   76         return;
   77     case IOPRIO_CLASS_RT:
   78     case IOPRIO_CLASS_BE:
   79         if (level < 0 || level > 7) {
   80             (void)fprintf(stderr, "Priority levels range from 0 "
   81                 "(max) to 7 (min)\n");
   82             _exit(EXIT_FAILURE);
   83         }
   84         break;
   85     case IOPRIO_CLASS_IDLE:
   86         if ((level != UNDEFINED) &&
   87             (level != 0))
   88             (void)fprintf(stderr, "Cannot set priority level "
   89                 "with idle, defaulting to 0\n");
   90         data = 0;
   91         break;
   92     default:
   93         (void)fprintf(stderr, "Unknown priority class: %d\n", class);
   94         _exit(EXIT_FAILURE);
   95     }
   96     rc = shim_ioprio_set(IOPRIO_WHO_PROCESS, 0,
   97         IOPRIO_PRIO_VALUE(class, data));
   98     if ((rc < 0) && (errno != ENOSYS)) {
   99         (void)fprintf(stderr, "Cannot set I/O priority: errno=%d (%s)\n",
  100             errno, strerror(errno));
  101         _exit(EXIT_FAILURE);
  102     }
  103 }
  104 #else
  105 void set_iopriority(const int32_t class, const int32_t level)
  106 {
  107     (void)class;
  108     (void)level;
  109 }
  110 #endif