~ubuntu-branches/ubuntu/jaunty/libgdiplus/jaunty-updates

« back to all changes in this revision

Viewing changes to cairo/perf/cairo-perf-posix.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2007-05-17 13:38:42 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20070517133842-t8b8d4lxmhb3vnp0
Tags: 1.2.4-1ubuntu1
* Sync with Debian:
  + debian/control:
    - Add sparc to archs
    - Set Maintainer field...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2006 Mozilla Corporation
 
3
 * Copyright © 2006 Red Hat, Inc.
 
4
 *
 
5
 * Permission to use, copy, modify, distribute, and sell this software
 
6
 * and its documentation for any purpose is hereby granted without
 
7
 * fee, provided that the above copyright notice appear in all copies
 
8
 * and that both that copyright notice and this permission notice
 
9
 * appear in supporting documentation, and that the name of
 
10
 * the authors not be used in advertising or publicity pertaining to
 
11
 * distribution of the software without specific, written prior
 
12
 * permission. The authors make no representations about the
 
13
 * suitability of this software for any purpose.  It is provided "as
 
14
 * is" without express or implied warranty.
 
15
 *
 
16
 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 
17
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 
18
 * FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
 
19
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 
20
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 
21
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 
22
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
23
 *
 
24
 * Authors: Vladimir Vukicevic <vladimir@pobox.com>
 
25
 *          Carl Worth <cworth@cworth.org>
 
26
 */
 
27
 
 
28
/* Portions of this file come from liboil:
 
29
 *
 
30
 * LIBOIL - Library of Optimized Inner Loops
 
31
 * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
 
32
 * All rights reserved.
 
33
 *
 
34
 * Redistribution and use in source and binary forms, with or without
 
35
 * modification, are permitted provided that the following conditions
 
36
 * are met:
 
37
 * 1. Redistributions of source code must retain the above copyright
 
38
 *    notice, this list of conditions and the following disclaimer.
 
39
 * 2. Redistributions in binary form must reproduce the above copyright
 
40
 *    notice, this list of conditions and the following disclaimer in the
 
41
 *    documentation and/or other materials provided with the distribution.
 
42
 *
 
43
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 
44
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
45
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
46
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 
47
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
48
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
49
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
50
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 
51
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 
52
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
53
 * POSSIBILITY OF SUCH DAMAGE.
 
54
 */
 
55
 
 
56
#include <signal.h>
 
57
#include <sys/time.h>
 
58
#include <unistd.h>
 
59
#ifdef _POSIX_PRIORITY_SCHEDULING
 
60
#include <sched.h>
 
61
#endif
 
62
 
 
63
#include "cairo-perf.h"
 
64
 
 
65
/* timers */
 
66
 
 
67
#if defined(__i386__) || defined(__amd64__)
 
68
static inline unsigned long
 
69
oil_profile_stamp_rdtsc (void)
 
70
{
 
71
    unsigned long ts;
 
72
    __asm__ __volatile__("rdtsc\n" : "=a" (ts) : : "edx");
 
73
    return ts;
 
74
}
 
75
#define OIL_STAMP oil_profile_stamp_rdtsc
 
76
#endif
 
77
 
 
78
#if defined(__powerpc__) || defined(__PPC__) || defined(__ppc__)
 
79
static inline cairo_perf_ticks_t
 
80
oil_profile_stamp_tb(void)
 
81
{
 
82
    unsigned long ts;
 
83
    __asm__ __volatile__("mftb %0\n" : "=r" (ts));
 
84
    return ts;
 
85
}
 
86
#define OIL_STAMP oil_profile_stamp_tb
 
87
#endif
 
88
 
 
89
#if defined(__alpha__)
 
90
static inline cairo_perf_ticks_t
 
91
oil_profile_stamp_alpha(void)
 
92
{
 
93
    unsigned int ts;
 
94
    __asm__ __volatile__ ("rpcc %0\n" : "=r"(ts));
 
95
    return ts;
 
96
}
 
97
#define OIL_STAMP oil_profile_stamp_alpha
 
98
#endif
 
99
 
 
100
#if defined(__s390__)
 
101
static cairo_perf_ticks_t
 
102
oil_profile_stamp_s390(void)
 
103
{
 
104
    uint64_t ts;
 
105
    __asm__ __volatile__ ("STCK %0\n" : : "m" (ts));
 
106
    return ts;
 
107
}
 
108
#define OIL_STAMP oil_profile_stamp_s390
 
109
#endif
 
110
 
 
111
typedef struct _cairo_perf_timer
 
112
{
 
113
#ifdef OIL_STAMP
 
114
    cairo_perf_ticks_t start;
 
115
    cairo_perf_ticks_t stop;
 
116
#else
 
117
    struct timeval tv_start;
 
118
    struct timeval tv_stop;
 
119
#endif
 
120
} cairo_perf_timer_t;
 
121
 
 
122
static cairo_perf_timer_t timer;
 
123
 
 
124
static cairo_perf_timer_synchronize_t cairo_perf_timer_synchronize = NULL;
 
125
static void *cairo_perf_timer_synchronize_closure = NULL;
 
126
void
 
127
cairo_perf_timer_set_synchronize (cairo_perf_timer_synchronize_t         synchronize,
 
128
                                  void                                  *closure)
 
129
{
 
130
    cairo_perf_timer_synchronize = synchronize;
 
131
    cairo_perf_timer_synchronize_closure = closure;
 
132
}
 
133
 
 
134
void
 
135
cairo_perf_timer_start (void) {
 
136
    if (cairo_perf_timer_synchronize)
 
137
        cairo_perf_timer_synchronize (cairo_perf_timer_synchronize_closure);
 
138
#ifdef OIL_STAMP
 
139
    timer.start = OIL_STAMP ();
 
140
#else
 
141
    gettimeofday (&timer.tv_start, NULL);
 
142
#endif
 
143
}
 
144
 
 
145
void
 
146
cairo_perf_timer_stop (void) {
 
147
    if (cairo_perf_timer_synchronize)
 
148
        cairo_perf_timer_synchronize (cairo_perf_timer_synchronize_closure);
 
149
#ifdef OIL_STAMP
 
150
    timer.stop = OIL_STAMP ();
 
151
#else
 
152
    gettimeofday (&timer.tv_stop, NULL);
 
153
#endif
 
154
}
 
155
 
 
156
cairo_perf_ticks_t
 
157
cairo_perf_timer_elapsed (void) {
 
158
    cairo_perf_ticks_t ticks;
 
159
 
 
160
#ifdef OIL_STAMP
 
161
    /* Handle 32-bit wraparound of timer value */
 
162
    if (timer.stop < timer.start)
 
163
        timer.stop += 0x100000000ll;
 
164
    ticks = (timer.stop - timer.start);
 
165
#else
 
166
    ticks = (timer.tv_stop.tv_sec - timer.tv_start.tv_sec) * 1000000;
 
167
    ticks += (timer.tv_stop.tv_usec - timer.tv_start.tv_usec);
 
168
#endif
 
169
 
 
170
    return ticks;
 
171
}
 
172
 
 
173
cairo_perf_ticks_t
 
174
cairo_perf_ticks_per_second (void) {
 
175
#ifdef OIL_STAMP
 
176
    static cairo_perf_ticks_t tps = 0;
 
177
    /* XXX: This is obviously not stable in light of changing CPU speed. */
 
178
    if (tps == 0) {
 
179
        struct timeval tv_start, tv_stop;
 
180
        double tv_elapsed;
 
181
        cairo_perf_timer_start ();
 
182
        gettimeofday (&tv_start, NULL);
 
183
        usleep (20000);
 
184
        cairo_perf_timer_stop ();
 
185
        gettimeofday (&tv_stop, NULL);
 
186
        tv_elapsed = ((tv_stop.tv_sec - tv_start.tv_sec) +
 
187
                      + (tv_stop.tv_usec - tv_start.tv_usec) / 1000000.0);
 
188
        tps = round (cairo_perf_timer_elapsed () / tv_elapsed);
 
189
    }
 
190
    return tps;
 
191
#else
 
192
    /* For gettimeofday the units are micro-seconds */
 
193
    return 1000000;
 
194
#endif
 
195
}
 
196
 
 
197
/* yield */
 
198
 
 
199
void
 
200
cairo_perf_yield (void) {
 
201
#ifdef _POSIX_PRIORITY_SCHEDULING
 
202
    sched_yield ();
 
203
#endif
 
204
}