Tizen 2.1 base
[platform/upstream/gcd.git] / pthread_workqueue-0.8.2 / testing / latency / latency.h
1 /*
2  * Copyright (c) 2011 Joakim Johansson <jocke@tbricks.com>.
3  *
4  * @APPLE_APACHE_LICENSE_HEADER_START@
5  * 
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  * 
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  * 
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * 
18  * @APPLE_APACHE_LICENSE_HEADER_END@
19  */
20
21 #include "pthread_workqueue.h"
22
23 #ifdef _WIN32
24 # include "../../src/windows/platform.h"
25 #endif
26
27 // Run settings
28 #define SECONDS_TO_RUN 10
29 #define WORKQUEUE_COUNT 3
30 #define GENERATOR_WORKQUEUE_COUNT 1
31 #define SLEEP_BEFORE_START 0
32 #define FORCE_BUSY_LOOP 0
33 #define LATENCY_RUN_GENERATOR_IN_MAIN_THREAD 0
34
35 // Data rates
36 #define EVENTS_GENERATED_PER_TICK 100   // simulate some small bursting 
37 #define EVENT_GENERATION_FREQUENCY 100  // events/s base rate, need to use busy loop = 1 if > 100Hz due to nanosleep resolution 
38
39 #define AGGREGATE_DATA_RATE_PER_SECOND (EVENT_GENERATION_FREQUENCY * EVENTS_GENERATED_PER_TICK)
40 #define EVENTS_TO_GENERATE (SECONDS_TO_RUN * AGGREGATE_DATA_RATE_PER_SECOND)
41 #define TOTAL_DATA_PER_SECOND (AGGREGATE_DATA_RATE_PER_SECOND*GENERATOR_WORKQUEUE_COUNT)
42 #define TOTAL_TICKS_TO_RUN (SECONDS_TO_RUN * EVENT_GENERATION_FREQUENCY)
43
44 #define NANOSECONDS_PER_SECOND 1000000000
45 #define DISTRIBUTION_BUCKETS 20 // 1us per bucket
46 #define EVENT_TIME_SLICE (NANOSECONDS_PER_SECOND / EVENT_GENERATION_FREQUENCY)
47 #define SYSTEM_CLOCK_RESOLUTION 100
48
49 #ifdef _WIN32
50 typedef unsigned long long mytime_t;
51 #else
52 typedef unsigned long mytime_t;
53 #endif
54
55 struct wq_event 
56 {
57         unsigned int queue_index; 
58         mytime_t start_time;
59 };
60
61 struct wq_statistics 
62 {
63         mytime_t min; 
64         mytime_t max; 
65         double avg; 
66         mytime_t total; 
67         unsigned int count; 
68         unsigned int count_over_threshold; 
69     unsigned int distribution[DISTRIBUTION_BUCKETS];
70 };
71
72 // We create our own separate workqueues for event generation
73 struct wq_event_generator 
74 {
75         pthread_workqueue_t wq; 
76         struct wq_event *wq_events; 
77 };
78
79 #ifdef __sun
80 # include <atomic.h>
81 # define atomic_inc      atomic_inc_32
82 # define atomic_dec      atomic_dec_32
83 # define atomic_inc_nv   atomic_inc_32_nv
84 # define atomic_dec_nv   atomic_dec_32_nv
85 #elif defined(_WIN32)
86 # define atomic_inc(p)   (void) InterlockedIncrement((p))
87 # define atomic_dec(p)   (void) InterlockedDecrement((p))
88 # define atomic_inc_nv(p)  InterlockedIncrement((p))
89 # define atomic_dec_nv(p)  InterlockedDecrement((p))
90 #else
91 # define atomic_inc(p)   (void) __sync_add_and_fetch((p), 1)
92 # define atomic_dec(p)   (void) __sync_sub_and_fetch((p), 1)
93 # define atomic_inc_nv(p)  __sync_add_and_fetch((p), 1)
94 # define atomic_dec_nv(p)  __sync_sub_and_fetch((p), 1)
95 #endif