"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-ioprio.c" (15 Mar 2019, 5368 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 "stress-ioprio.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.50_vs_0.09.51.

    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 #if defined(HAVE_IOPRIO_GET) && \
   28     defined(HAVE_IOPRIO_SET) && \
   29     defined(HAVE_PWRITEV)
   30 
   31 #define MAX_IOV     (4)
   32 #define BUF_SIZE    (32)
   33 
   34 /*
   35  *  stress set/get io priorities
   36  *  stress system by rapid io priority changes
   37  */
   38 static int stress_ioprio(const args_t *args)
   39 {
   40     const uid_t uid = getuid();
   41 #if defined(HAVE_GETPGRP)
   42     const pid_t grp = getpgrp();
   43 #endif
   44     int fd, rc = EXIT_FAILURE;
   45     char filename[PATH_MAX];
   46 
   47     if (stress_temp_dir_mk_args(args) < 0)
   48         return rc;
   49 
   50     (void)stress_temp_filename_args(args,
   51         filename, sizeof(filename), mwc32());
   52     if ((fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
   53         rc = exit_status(errno);
   54         pr_fail_err("open");
   55         goto cleanup_dir;
   56     }
   57     (void)unlink(filename);
   58 
   59     do {
   60         int i;
   61         struct iovec iov[MAX_IOV];
   62         char buffer[MAX_IOV][BUF_SIZE];
   63 
   64         if (shim_ioprio_get(IOPRIO_WHO_PROCESS, args->pid) < 0) {
   65             if (errno != EINVAL) {
   66                 pr_fail("%s: ioprio_get(OPRIO_WHO_PROCESS, %d), "
   67                     "errno = %d (%s)\n",
   68                     args->name, args->pid, errno, strerror(errno));
   69                 goto cleanup_file;
   70             }
   71         }
   72         if (shim_ioprio_get(IOPRIO_WHO_PROCESS, 0) < 0) {
   73             if (errno != EINVAL) {
   74                 pr_fail("%s: ioprio_get(OPRIO_WHO_PROCESS, 0), "
   75                     "errno = %d (%s)\n",
   76                     args->name, errno, strerror(errno));
   77                 goto cleanup_file;
   78             }
   79         }
   80 #if defined(HAVE_GETPGRP)
   81         if (shim_ioprio_get(IOPRIO_WHO_PGRP, grp) < 0) {
   82             if (errno != EINVAL) {
   83                 pr_fail("%s: ioprio_get(OPRIO_WHO_PGRP, %d), "
   84                     "errno = %d (%s)\n",
   85                     args->name, g_pgrp, errno, strerror(errno));
   86                 goto cleanup_file;  
   87             }
   88         }
   89 #endif
   90         if (shim_ioprio_get(IOPRIO_WHO_PGRP, 0) < 0) {
   91             if (errno != EINVAL) {
   92                 pr_fail("%s: ioprio_get(OPRIO_WHO_PGRP, 0), "
   93                     "errno = %d (%s)\n",
   94                     args->name, errno, strerror(errno));
   95                 goto cleanup_file;
   96             }
   97         }
   98         if (shim_ioprio_get(IOPRIO_WHO_USER, uid) < 0) {
   99             if (errno != EINVAL) {
  100                 pr_fail("%s: ioprio_get(OPRIO_WHO_USR, %d), "
  101                     "errno = %d (%s)\n",
  102                     args->name, uid, errno, strerror(errno));
  103                 goto cleanup_file;
  104             }
  105         }
  106 
  107         for (i = 0; i < MAX_IOV; i++) {
  108             (void)memset(buffer[i], mwc8(), BUF_SIZE);
  109             iov[i].iov_base = buffer[i];
  110             iov[i].iov_len = BUF_SIZE;
  111         }
  112 
  113         if (pwritev(fd, iov, MAX_IOV, (off_t)512 * mwc16()) < 0) {
  114             pr_fail_err("pwritev");
  115             goto cleanup_file;
  116         }
  117         (void)shim_fsync(fd);
  118 
  119         if (shim_ioprio_set(IOPRIO_WHO_PROCESS, args->pid,
  120             IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)) < 0) {
  121             if ((errno != EPERM) && (errno != EINVAL)) {
  122                 pr_fail("%s: ioprio_set("
  123                     "IOPRIO_WHO_PROCESS, %d, "
  124                     "(IOPRIO_CLASS_IDLE, 0)), "
  125                     "errno = %d (%s)\n",
  126                     args->name, args->pid, errno, strerror(errno));
  127                 goto cleanup_file;
  128             }
  129         }
  130 
  131         if (pwritev(fd, iov, MAX_IOV, (off_t)512 * mwc16()) < 0) {
  132             pr_fail_err("pwritev");
  133             goto cleanup_file;
  134         }
  135         (void)shim_fsync(fd);
  136 
  137         for (i = 0; i < 8; i++) {
  138             if (shim_ioprio_set(IOPRIO_WHO_PROCESS, args->pid,
  139                 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, i)) < 0) {
  140                 if ((errno != EPERM) && (errno != EINVAL)) {
  141                     pr_fail("%s: ioprio_set("
  142                         "IOPRIO_WHO_PROCESS, %d, "
  143                         "(IOPRIO_CLASS_BE, %d)), "
  144                         "errno = %d (%s)\n",
  145                         args->name, args->pid, i, errno, strerror(errno));
  146                     goto cleanup_file;
  147                 }
  148             }
  149             if (pwritev(fd, iov, MAX_IOV, (off_t)512 * mwc16()) < 0) {
  150                 pr_fail_err("pwritev");
  151                 goto cleanup_file;
  152             }
  153             (void)shim_fsync(fd);
  154         }
  155         for (i = 0; i < 8; i++) {
  156             if (shim_ioprio_set(IOPRIO_WHO_PROCESS, args->pid,
  157                 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, i)) < 0) {
  158                 if ((errno != EPERM) && (errno != EINVAL)) {
  159                     pr_fail("%s: ioprio_set("
  160                         "IOPRIO_WHO_PROCESS, %d, "
  161                         "(IOPRIO_CLASS_RT, %d)), "
  162                         "errno = %d (%s)\n",
  163                         args->name, args->pid, i, errno, strerror(errno));
  164                     goto cleanup_file;
  165                 }
  166             }
  167             if (pwritev(fd, iov, MAX_IOV, (off_t)512 * mwc16()) < 0) {
  168                 pr_fail_err("pwritev");
  169                 goto cleanup_file;
  170             }
  171             (void)shim_fsync(fd);
  172         }
  173         inc_counter(args);
  174     } while (keep_stressing());
  175 
  176     rc = EXIT_SUCCESS;
  177 
  178 cleanup_file:
  179     (void)close(fd);
  180 cleanup_dir:
  181     (void)stress_temp_dir_rm_args(args);
  182 
  183     return rc;
  184 }
  185 
  186 stressor_info_t stress_ioprio_info = {
  187     .stressor = stress_ioprio,
  188     .class = CLASS_FILESYSTEM | CLASS_OS
  189 };
  190 #else
  191 stressor_info_t stress_ioprio_info = {
  192     .stressor = stress_not_implemented,
  193     .class = CLASS_FILESYSTEM | CLASS_OS
  194 };
  195 #endif