JWS C Library
C language utility library
time.c
Go to the documentation of this file.
00001 /*
00002  * This work is licensed under a Creative Commons 
00003  * Attribution-Noncommercial-Share Alike 3.0 United States License.
00004  * 
00005  *    http://creativecommons.org/licenses/by-nc-sa/3.0/us/
00006  * 
00007  * You are free:
00008  * 
00009  *    to Share - to copy, distribute, display, and perform the work
00010  *    to Remix - to make derivative works
00011  * 
00012  * Under the following conditions:
00013  * 
00014  *    Attribution. You must attribute the work in the manner specified by the
00015  *    author or licensor (but not in any way that suggests that they endorse you
00016  *    or your use of the work).
00017  * 
00018  *    Noncommercial. You may not use this work for commercial purposes.
00019  * 
00020  *    Share Alike. If you alter, transform, or build upon this work, you may
00021  *    distribute the resulting work only under the same or similar license to
00022  *    this one.
00023  * 
00024  * For any reuse or distribution, you must make clear to others the license
00025  * terms of this work. The best way to do this is by including this header.
00026  * 
00027  * Any of the above conditions can be waived if you get permission from the
00028  * copyright holder.
00029  * 
00030  * Apart from the remix rights granted under this license, nothing in this
00031  * license impairs or restricts the author's moral rights.
00032  */
00033 
00034 
00046 #include <jwsc/config.h>
00047 
00048 #include <stdlib.h>
00049 #include <stdio.h>
00050 #include <inttypes.h>
00051 #include <unistd.h>
00052 #include <sys/times.h>
00053 
00054 #include "jwsc/base/time.h"
00055 
00056 
00060 void start_timer(Timer* t)
00061 {
00062     times(&(t->time));
00063 }
00064 
00065 
00069 void stop_timer(Timer* t)
00070 {
00071     struct tms now;
00072 
00073     times(&now);
00074 
00075     t->time.tms_utime  = now.tms_utime  - t->time.tms_utime;
00076     t->time.tms_stime  = now.tms_stime  - t->time.tms_stime;
00077     t->time.tms_cutime = now.tms_cutime - t->time.tms_cutime;
00078     t->time.tms_cstime = now.tms_cstime - t->time.tms_cstime;
00079 }
00080 
00081 
00086 void add_time_to_timer(Timer* t_1, const Timer* t_2)
00087 {
00088     t_1->time.tms_utime  += t_2->time.tms_utime;
00089     t_1->time.tms_stime  += t_2->time.tms_stime;
00090     t_1->time.tms_cutime += t_2->time.tms_cutime;
00091     t_1->time.tms_cstime += t_2->time.tms_cstime;
00092 }
00093 
00094 
00096 void zero_timer(Timer* t)
00097 {
00098     t->time.tms_utime  = 0;
00099     t->time.tms_stime  = 0;
00100     t->time.tms_cutime = 0;
00101     t->time.tms_cstime = 0;
00102 }
00103 
00104 
00112 double user_time_s(const Timer* t)
00113 {
00114     return (double)t->time.tms_utime / (double)sysconf(_SC_CLK_TCK);
00115 }
00116 
00117 
00118 double user_time_ms(const Timer* t)
00119 {
00120     return (double)(1000 * t->time.tms_utime) / 
00121            (double)sysconf(_SC_CLK_TCK);
00122 }
00123 
00134 double sys_time_s(const Timer* t)
00135 {
00136     return (double)t->time.tms_stime / (double)sysconf(_SC_CLK_TCK);
00137 }
00138 
00139 
00140 double sys_time_ms(const Timer* t)
00141 {
00142     return (double)(1000 * t->time.tms_stime) / 
00143            (double)sysconf(_SC_CLK_TCK);
00144 }
00145 
00156 double proc_user_time_s()
00157 {
00158     struct tms now;
00159 
00160     times(&now);
00161 
00162     return (double)now.tms_utime / (double)sysconf(_SC_CLK_TCK);
00163 }
00164 
00165 
00166 double proc_user_time_ms()
00167 {
00168     struct tms now;
00169 
00170     times(&now);
00171 
00172     return (double)(1000 * now.tms_utime) / (double)sysconf(_SC_CLK_TCK);
00173 }
00174 
00185 double proc_sys_time_s()
00186 {
00187     struct tms now;
00188 
00189     times(&now);
00190 
00191     return (double)now.tms_stime / (double)sysconf(_SC_CLK_TCK);
00192 }
00193 
00194 
00195 double proc_sys_time_ms()
00196 {
00197     struct tms now;
00198 
00199     times(&now);
00200 
00201     return (double)(1000 * now.tms_stime) / (double)sysconf(_SC_CLK_TCK);
00202 }
00203