"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/core-time.c" (15 Mar 2019, 2956 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-time.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 #define SECONDS_IN_MINUTE   (60.0)
   28 #define SECONDS_IN_HOUR     (60.0 * SECONDS_IN_MINUTE)
   29 #define SECONDS_IN_DAY      (24.0 * SECONDS_IN_HOUR)
   30 #define SECONDS_IN_YEAR     (365.2425 * SECONDS_IN_DAY)
   31                 /* Approx, for Gregorian calendar */
   32 /*
   33  *  time_now()
   34  *  time in seconds as a double
   35  */
   36 double time_now(void)
   37 {
   38     struct timeval now;
   39 
   40     if (gettimeofday(&now, NULL) < 0)
   41         return -1.0;
   42 
   43     return timeval_to_double(&now);
   44 }
   45 
   46 /*
   47  *  format_time()
   48  *  format a unit of time into human readable format
   49  */
   50 static inline void format_time(
   51     const bool last,        /* Last unit to format */
   52     const double secs_in_units, /* Seconds in the specific time unit */
   53     const char *units,      /* Unit of time */
   54     char **ptr,         /* Destination string ptr */
   55     double *duration,       /* Duration left in seconds */
   56     size_t *len)            /* Length of string left at ptr */
   57 {
   58     unsigned long val = (unsigned long)(*duration / secs_in_units);
   59 
   60     if (last || val > 0) {
   61         int ret;
   62 
   63         if (last)
   64             ret = snprintf(*ptr, *len, "%.2f %ss", *duration, units);
   65         else
   66             ret = snprintf(*ptr, *len, "%lu %s%s, ", val, units,
   67                 (val > 1) ? "s" : "");
   68         if (ret > 0) {
   69             *len -= ret;
   70             *ptr += ret;
   71         }
   72     }
   73     *duration -= secs_in_units * (double)val;
   74 }
   75 
   76 /*
   77  *  duration_to_str
   78  *  duration in seconds to a human readable string
   79  */
   80 const char *duration_to_str(const double duration)
   81 {
   82     static char str[128];
   83     char *ptr = str;
   84     size_t len = sizeof(str) - 1;
   85     double dur = duration;
   86 
   87     *str = '\0';
   88     if (duration > 60.0) {
   89         (void)shim_strlcpy(ptr, " (", len);
   90         ptr += 2;
   91         len -= 2;
   92         format_time(false, SECONDS_IN_YEAR, "year", &ptr, &dur, &len);
   93         format_time(false, SECONDS_IN_DAY, "day", &ptr, &dur, &len);
   94         format_time(false, SECONDS_IN_HOUR, "hour", &ptr, &dur, &len);
   95         format_time(false, SECONDS_IN_MINUTE, "min", &ptr, &dur, &len);
   96         format_time(true, 1, "sec", &ptr, &dur, &len);
   97         (void)shim_strlcpy(ptr, ")", len);
   98     }
   99     return str;
  100 }