"Fossies" - the Fresh Open Source Software Archive

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

    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_LOCKF)
   28 
   29 #define LOCK_FILE_SIZE  (64 * 1024)
   30 #define LOCK_SIZE   (8)
   31 #define LOCK_MAX    (1024)
   32 
   33 typedef struct lockf_info {
   34     off_t   offset;
   35     struct lockf_info *next;
   36 } lockf_info_t;
   37 
   38 typedef struct {
   39     lockf_info_t *head;     /* Head of lockf_info procs list */
   40     lockf_info_t *tail;     /* Tail of lockf_info procs list */
   41     lockf_info_t *free;     /* List of free'd lockf_infos */
   42     uint64_t length;        /* Length of list */
   43 } lockf_info_list_t;
   44 
   45 static lockf_info_list_t lockf_infos;
   46 
   47 /*
   48  *  stress_lockf_info_new()
   49  *  allocate a new lockf_info, add to end of list
   50  */
   51 static lockf_info_t *stress_lockf_info_new(void)
   52 {
   53     lockf_info_t *new;
   54 
   55     if (lockf_infos.free) {
   56         /* Pop an old one off the free list */
   57         new = lockf_infos.free;
   58         lockf_infos.free = new->next;
   59         new->next = NULL;
   60     } else {
   61         new = calloc(1, sizeof(*new));
   62         if (!new)
   63             return NULL;
   64     }
   65 
   66     if (lockf_infos.head)
   67         lockf_infos.tail->next = new;
   68     else
   69         lockf_infos.head = new;
   70 
   71     lockf_infos.tail = new;
   72     lockf_infos.length++;
   73 
   74     return new;
   75 }
   76 
   77 /*
   78  *  stress_lockf_info_head_remove
   79  *  reap a lockf_info and remove a lockf_info from head of list, put it onto
   80  *  the free lockf_info list
   81  */
   82 static void stress_lockf_info_head_remove(void)
   83 {
   84     if (lockf_infos.head) {
   85         lockf_info_t *head = lockf_infos.head;
   86 
   87         if (lockf_infos.tail == lockf_infos.head) {
   88             lockf_infos.tail = NULL;
   89             lockf_infos.head = NULL;
   90         } else {
   91             lockf_infos.head = head->next;
   92         }
   93 
   94         /* Shove it on the free list */
   95         head->next = lockf_infos.free;
   96         lockf_infos.free = head;
   97 
   98         lockf_infos.length--;
   99     }
  100 }
  101 
  102 /*
  103  *  stress_lockf_info_free()
  104  *  free the lockf_infos off the lockf_info head and free lists
  105  */
  106 static void stress_lockf_info_free(void)
  107 {
  108     while (lockf_infos.head) {
  109         lockf_info_t *next = lockf_infos.head->next;
  110 
  111         free(lockf_infos.head);
  112         lockf_infos.head = next;
  113     }
  114 
  115     while (lockf_infos.free) {
  116         lockf_info_t *next = lockf_infos.free->next;
  117 
  118         free(lockf_infos.free);
  119         lockf_infos.free = next;
  120     }
  121 }
  122 
  123 /*
  124  *  stress_lockf_unlock()
  125  *  pop oldest lock record off list and unlock it
  126  */
  127 static int stress_lockf_unlock(const args_t *args, const int fd)
  128 {
  129     /* Pop one off list */
  130     if (!lockf_infos.head)
  131         return 0;
  132 
  133     if (lseek(fd, lockf_infos.head->offset, SEEK_SET) < 0) {
  134         pr_fail_err("lseek");
  135         return -1;
  136     }
  137     stress_lockf_info_head_remove();
  138 
  139     if (lockf(fd, F_ULOCK, LOCK_SIZE) < 0) {
  140         pr_fail_err("lockf unlock");
  141         return -1;
  142     }
  143     return 0;
  144 }
  145 
  146 /*
  147  *  stress_lockf_contention()
  148  *  hammer lock/unlock to create some file lock contention
  149  */
  150 static int stress_lockf_contention(
  151     const args_t *args,
  152     const int fd)
  153 {
  154     const int lockf_cmd = (g_opt_flags & OPT_FLAGS_LOCKF_NONBLK) ?
  155         F_TLOCK : F_LOCK;
  156 
  157     mwc_reseed();
  158 
  159     do {
  160         off_t offset;
  161         int rc;
  162         lockf_info_t *lockf_info;
  163 
  164         if (lockf_infos.length >= LOCK_MAX)
  165             if (stress_lockf_unlock(args, fd) < 0)
  166                 return -1;
  167 
  168         offset = mwc64() % (LOCK_FILE_SIZE - LOCK_SIZE);
  169         if (lseek(fd, offset, SEEK_SET) < 0) {
  170             pr_fail_err("lseek");
  171             return -1;
  172         }
  173         rc = lockf(fd, lockf_cmd, LOCK_SIZE);
  174         if (rc < 0) {
  175             if (stress_lockf_unlock(args, fd) < 0)
  176                 return -1;
  177             continue;
  178         }
  179         /* Locked OK, add to lock list */
  180 
  181         lockf_info = stress_lockf_info_new();
  182         if (!lockf_info) {
  183             pr_fail_err("calloc");
  184             return -1;
  185         }
  186         lockf_info->offset = offset;
  187         inc_counter(args);
  188     } while (keep_stressing());
  189 
  190     return 0;
  191 }
  192 
  193 /*
  194  *  stress_lockf
  195  *  stress file locking via lockf()
  196  */
  197 static int stress_lockf(const args_t *args)
  198 {
  199     int fd, ret = EXIT_FAILURE;
  200     pid_t cpid = -1;
  201     char filename[PATH_MAX];
  202     char dirname[PATH_MAX];
  203     char buffer[4096];
  204     off_t offset;
  205     ssize_t rc;
  206 
  207     (void)memset(buffer, 0, sizeof(buffer));
  208 
  209     /*
  210      *  There will be a race to create the directory
  211      *  so EEXIST is expected on all but one instance
  212      */
  213     (void)stress_temp_dir_args(args, dirname, sizeof(dirname));
  214     if (mkdir(dirname, S_IRWXU) < 0) {
  215         if (errno != EEXIST) {
  216             ret = exit_status(errno);
  217             pr_fail_err("mkdir");
  218             return ret;
  219         }
  220     }
  221 
  222     /*
  223      *  Lock file is based on parent pid and instance 0
  224      *  as we need to share this among all the other
  225      *  stress flock processes
  226      */
  227     (void)stress_temp_filename_args(args,
  228         filename, sizeof(filename), mwc32());
  229 
  230     if ((fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
  231         ret = exit_status(errno);
  232         pr_fail_err("open");
  233         (void)rmdir(dirname);
  234         return ret;
  235     }
  236 
  237     if (lseek(fd, 0, SEEK_SET) < 0) {
  238         pr_fail_err("lseek");
  239         goto tidy;
  240     }
  241     for (offset = 0; offset < LOCK_FILE_SIZE; offset += sizeof(buffer)) {
  242 redo:
  243         if (!g_keep_stressing_flag) {
  244             ret = EXIT_SUCCESS;
  245             goto tidy;
  246         }
  247         rc = write(fd, buffer, sizeof(buffer));
  248         if ((rc < 0) || (rc != sizeof(buffer))) {
  249             if ((errno == EAGAIN) || (errno == EINTR))
  250                 goto redo;
  251             ret = exit_status(errno);
  252             pr_fail_err("write");
  253             goto tidy;
  254         }
  255     }
  256 
  257 again:
  258     cpid = fork();
  259     if (cpid < 0) {
  260         if (!g_keep_stressing_flag) {
  261             ret = EXIT_SUCCESS;
  262             goto tidy;
  263         }
  264         if ((errno == EAGAIN) || (errno == ENOMEM))
  265             goto again;
  266         pr_fail_err("fork");
  267         goto tidy;
  268     }
  269     if (cpid == 0) {
  270         (void)setpgid(0, g_pgrp);
  271         stress_parent_died_alarm();
  272 
  273         if (stress_lockf_contention(args, fd) < 0)
  274             _exit(EXIT_FAILURE);
  275         stress_lockf_info_free();
  276         _exit(EXIT_SUCCESS);
  277     }
  278     (void)setpgid(cpid, g_pgrp);
  279 
  280     if (stress_lockf_contention(args, fd) == 0)
  281         ret = EXIT_SUCCESS;
  282 tidy:
  283     if (cpid > 0) {
  284         int status;
  285 
  286         (void)kill(cpid, SIGKILL);
  287         (void)waitpid(cpid, &status, 0);
  288     }
  289     stress_lockf_info_free();
  290 
  291     (void)close(fd);
  292     (void)unlink(filename);
  293     (void)rmdir(dirname);
  294 
  295     return ret;
  296 }
  297 
  298 stressor_info_t stress_lockf_info = {
  299     .stressor = stress_lockf,
  300     .class = CLASS_FILESYSTEM | CLASS_OS
  301 };
  302 #else
  303 stressor_info_t stress_lockf_info = {
  304     .stressor = stress_not_implemented,
  305     .class = CLASS_FILESYSTEM | CLASS_OS
  306 };
  307 #endif