]> git.saurik.com Git - apple/libdispatch.git/blob - src/shims/perfmon.h
libdispatch-913.1.6.tar.gz
[apple/libdispatch.git] / src / shims / perfmon.h
1 /*
2 * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
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 /*
22 * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch
23 * which are subject to change in future releases of Mac OS X. Any applications
24 * relying on these interfaces WILL break.
25 */
26
27 #ifndef __DISPATCH_SHIMS_PERFMON__
28 #define __DISPATCH_SHIMS_PERFMON__
29
30 #if DISPATCH_PERF_MON
31 #if DISPATCH_INTROSPECTION
32 #error invalid configuration
33 #endif
34
35 typedef enum {
36 perfmon_thread_no_trace = 0,
37 perfmon_thread_event_no_steal, // 1) Event threads that couldn't steal
38 perfmon_thread_event_steal, // 2) Event threads failing to steal very late
39 perfmon_thread_worker_non_oc, // 3) Non overcommit threads finding
40 // nothing on the root queues
41 perfmon_thread_worker_oc, // 4) Overcommit thread finding nothing to do
42 perfmon_thread_manager,
43 } perfmon_thread_type;
44
45 DISPATCH_ALWAYS_INLINE
46 static inline void
47 _dispatch_perfmon_workitem_inc(void)
48 {
49 unsigned long cnt;
50 cnt = (unsigned long)_dispatch_thread_getspecific(dispatch_bcounter_key);
51 _dispatch_thread_setspecific(dispatch_bcounter_key, (void *)++cnt);
52 }
53
54 DISPATCH_ALWAYS_INLINE
55 static inline void
56 _dispatch_perfmon_workitem_dec(void)
57 {
58 unsigned long cnt;
59 cnt = (unsigned long)_dispatch_thread_getspecific(dispatch_bcounter_key);
60 _dispatch_thread_setspecific(dispatch_bcounter_key, (void *)--cnt);
61 }
62
63 #define DISPATCH_PERF_MON_ARGS_PROTO , uint64_t perfmon_start
64 #define DISPATCH_PERF_MON_ARGS , perfmon_start
65 #define DISPATCH_PERF_MON_VAR uint64_t perfmon_start;
66 #define DISPATCH_PERF_MON_VAR_INIT uint64_t perfmon_start = 0;
67
68 #define _dispatch_perfmon_start_impl(trace) ({ \
69 if (trace) _dispatch_ktrace0(DISPATCH_PERF_MON_worker_thread_start); \
70 perfmon_start = _dispatch_absolute_time(); \
71 })
72 #define _dispatch_perfmon_start() \
73 DISPATCH_PERF_MON_VAR _dispatch_perfmon_start_impl(true)
74 #define _dispatch_perfmon_start_notrace() \
75 DISPATCH_PERF_MON_VAR _dispatch_perfmon_start_impl(false)
76 #define _dispatch_perfmon_end(thread_type) \
77 _dispatch_queue_merge_stats(perfmon_start, true, thread_type)
78 #define _dispatch_perfmon_end_notrace() \
79 _dispatch_queue_merge_stats(perfmon_start, false, perfmon_thread_no_trace)
80
81 void _dispatch_queue_merge_stats(uint64_t start, bool trace, perfmon_thread_type type);
82
83 #else
84
85 #define DISPATCH_PERF_MON_ARGS_PROTO
86 #define DISPATCH_PERF_MON_ARGS
87 #define DISPATCH_PERF_MON_VAR
88 #define DISPATCH_PERF_MON_VAR_INIT
89 #define _dispatch_perfmon_workitem_inc()
90 #define _dispatch_perfmon_workitem_dec()
91 #define _dispatch_perfmon_start_impl(trace)
92 #define _dispatch_perfmon_start()
93 #define _dispatch_perfmon_end(thread_type)
94 #define _dispatch_perfmon_start_notrace()
95 #define _dispatch_perfmon_end_notrace()
96
97 #endif // DISPATCH_PERF_MON
98
99 #endif