"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-context.c" (15 Mar 2019, 3742 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-context.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_SWAPCONTEXT) &&    \
   28     defined(HAVE_UCONTEXT_H)
   29 
   30 #define STACK_SIZE  (16384)
   31 
   32 static uint8_t stack_sig[SIGSTKSZ + STACK_ALIGNMENT];
   33 
   34 static ucontext_t uctx_main, uctx_thread1, uctx_thread2, uctx_thread3;
   35 static uint64_t __counter, __max_ops;
   36 
   37 static void thread1(void)
   38 {
   39     do {
   40         __counter++;
   41         (void)swapcontext(&uctx_thread1, &uctx_thread2);
   42     } while (g_keep_stressing_flag && (!__max_ops || __counter < __max_ops));
   43 }
   44 
   45 static void thread2(void)
   46 {
   47     do {
   48         __counter++;
   49         (void)swapcontext(&uctx_thread2, &uctx_thread3);
   50     } while (g_keep_stressing_flag && (!__max_ops || __counter < __max_ops));
   51 }
   52 
   53 static void thread3(void)
   54 {
   55     do {
   56         __counter++;
   57         (void)swapcontext(&uctx_thread3, &uctx_thread1);
   58     } while (g_keep_stressing_flag && (!__max_ops || __counter < __max_ops));
   59 }
   60 
   61 static int stress_context_init(
   62     const args_t *args,
   63     void (*func)(void),
   64     ucontext_t *uctx_link,
   65     ucontext_t *uctx,
   66     void *stack,
   67     const size_t stack_size)
   68 {
   69     if (getcontext(uctx) < 0) {
   70         pr_err("%s: getcontext failed: %d (%s)\n",
   71             args->name, errno, strerror(errno));
   72         return -1;
   73     }
   74     uctx->uc_stack.ss_sp = (void *)stack;
   75     uctx->uc_stack.ss_size = stack_size;
   76     uctx->uc_link = uctx_link;
   77     makecontext(uctx, func, 0);
   78 
   79     return 0;
   80 }
   81 
   82 /*
   83  *  stress_context()
   84  *  stress that exercises CPU context save/restore
   85  */
   86 static int stress_context(const args_t *args)
   87 {
   88     char stack_thread1[STACK_SIZE + STACK_ALIGNMENT],
   89          stack_thread2[STACK_SIZE + STACK_ALIGNMENT],
   90          stack_thread3[STACK_SIZE + STACK_ALIGNMENT];
   91 
   92     if (stress_sigaltstack(stack_sig, SIGSTKSZ) < 0)
   93         return EXIT_FAILURE;
   94 
   95     __counter = 0;
   96     __max_ops = args->max_ops * 1000;
   97 
   98     /* Create 3 micro threads */
   99     if (stress_context_init(args, thread1, &uctx_main,
  100                 &uctx_thread1,
  101                 stress_align_address(stack_thread1, STACK_ALIGNMENT),
  102                 STACK_SIZE) < 0)
  103         return EXIT_FAILURE;
  104     if (stress_context_init(args, thread2, &uctx_main,
  105                 &uctx_thread2,
  106                 stress_align_address(stack_thread2, STACK_ALIGNMENT),
  107                 STACK_SIZE) < 0)
  108         return EXIT_FAILURE;
  109     if (stress_context_init(args, thread3, &uctx_main,
  110                 &uctx_thread3,
  111                 stress_align_address(stack_thread3, STACK_ALIGNMENT),
  112                 STACK_SIZE) < 0)
  113         return EXIT_FAILURE;
  114 
  115     /* And start.. */
  116     if (swapcontext(&uctx_main, &uctx_thread1) < 0) {
  117         pr_err("%s: swapcontext failed: %d (%s)\n",
  118             args->name, errno, strerror(errno));
  119         return EXIT_FAILURE;
  120     }
  121 
  122     set_counter(args, __counter / 1000);
  123 
  124     return EXIT_SUCCESS;
  125 }
  126 
  127 stressor_info_t stress_context_info = {
  128     .stressor = stress_context,
  129     .class = CLASS_MEMORY | CLASS_CPU
  130 };
  131 #else
  132 stressor_info_t stress_context_info = {
  133     .stressor = stress_not_implemented,
  134     .class = CLASS_MEMORY | CLASS_CPU
  135 };
  136 #endif