OSDN Git Service

Merge remote branch 'goog/dalvik-dev' into dalvik-dev-to-master
[android-x86/dalvik.git] / vm / Misc.h
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Miscellaneous utility functions.
19  */
20 #ifndef _DALVIK_MISC
21 #define _DALVIK_MISC
22
23 #include "Inlines.h"
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/time.h>
28
29 /*
30  * Used to shut up the compiler when a parameter isn't used.
31  */
32 #define UNUSED_PARAMETER(p)     (void)(p)
33
34 /*
35  * Floating point conversion functions.  These are necessary to avoid
36  * strict-aliasing problems ("dereferencing type-punned pointer will break
37  * strict-aliasing rules").  According to the gcc info page, this usage
38  * is allowed, even with "-fstrict-aliasing".
39  *
40  * The code generated by gcc-4.1.1 appears to be much better than a
41  * type cast dereference ("int foo = *(int*)&myfloat") when the conversion
42  * function is inlined.  It also allows us to take advantage of the
43  * optimizations that strict aliasing rules allow.
44  */
45 INLINE float dvmU4ToFloat(u4 val) {
46     union { u4 in; float out; } conv;
47     conv.in = val;
48     return conv.out;
49 }
50 INLINE u4 dvmFloatToU4(float val) {
51     union { float in; u4 out; } conv;
52     conv.in = val;
53     return conv.out;
54 }
55
56 /*
57  * Print a hex dump to the log file.
58  *
59  * "local" mode prints a hex dump starting from offset 0 (roughly equivalent
60  * to "xxd -g1").
61  *
62  * "mem" mode shows the actual memory address, and will offset the start
63  * so that the low nibble of the address is always zero.
64  *
65  * If "tag" is NULL the default tag ("dalvikvm") will be used.
66  */
67 typedef enum { kHexDumpLocal, kHexDumpMem } HexDumpMode;
68 void dvmPrintHexDumpEx(int priority, const char* tag, const void* vaddr,
69     size_t length, HexDumpMode mode);
70
71 /*
72  * Print a hex dump, at INFO level.
73  */
74 INLINE void dvmPrintHexDump(const void* vaddr, size_t length) {
75     dvmPrintHexDumpEx(ANDROID_LOG_INFO, LOG_TAG,
76         vaddr, length, kHexDumpLocal);
77 }
78
79 /*
80  * Print a hex dump at VERBOSE level. This does nothing in non-debug builds.
81  */
82 INLINE void dvmPrintHexDumpDbg(const void* vaddr, size_t length,const char* tag)
83 {
84 #if !LOG_NDEBUG
85     dvmPrintHexDumpEx(ANDROID_LOG_VERBOSE, (tag != NULL) ? tag : LOG_TAG,
86         vaddr, length, kHexDumpLocal);
87 #endif
88 }
89
90 /*
91  * We pass one of these around when we want code to be able to write debug
92  * info to either the log or to a file (or stdout/stderr).
93  */
94 typedef struct DebugOutputTarget {
95     /* where to? */
96     enum {
97         kDebugTargetUnknown = 0,
98         kDebugTargetLog,
99         kDebugTargetFile,
100     } which;
101
102     /* additional bits */
103     union {
104         struct {
105             int priority;
106             const char* tag;
107         } log;
108         struct {
109             FILE* fp;
110         } file;
111     } data;
112 } DebugOutputTarget;
113
114 /*
115  * Fill in a DebugOutputTarget struct.
116  */
117 void dvmCreateLogOutputTarget(DebugOutputTarget* target, int priority,
118     const char* tag);
119 void dvmCreateFileOutputTarget(DebugOutputTarget* target, FILE* fp);
120
121 /*
122  * Print a debug message.
123  */
124 void dvmPrintDebugMessage(const DebugOutputTarget* target, const char* format,
125     ...)
126 #if defined(__GNUC__)
127     __attribute__ ((format(printf, 2, 3)))
128 #endif
129     ;
130
131 /*
132  * Return a newly-allocated string in which all occurrences of '.' have
133  * been changed to '/'.  If we find a '/' in the original string, NULL
134  * is returned to avoid ambiguity.
135  */
136 char* dvmDotToSlash(const char* str);
137
138 /*
139  * Return a newly-allocated string containing a human-readable equivalent
140  * of 'descriptor'. So "I" would be "int", "[[I" would be "int[][]",
141  * "[Ljava/lang/String;" would be "java.lang.String[]", and so forth.
142  */
143 char* dvmHumanReadableDescriptor(const char* descriptor);
144
145 /*
146  * Return a newly-allocated string for the "dot version" of the class
147  * name for the given type descriptor. That is, The initial "L" and
148  * final ";" (if any) have been removed and all occurrences of '/'
149  * have been changed to '.'.
150  *
151  * "Dot version" names are used in the class loading machinery.
152  * See also dvmHumanReadableDescriptor.
153  */
154 char* dvmDescriptorToDot(const char* str);
155
156 /*
157  * Return a newly-allocated string for the type descriptor
158  * corresponding to the "dot version" of the given class name. That
159  * is, non-array names are surrounded by "L" and ";", and all
160  * occurrences of '.' have been changed to '/'.
161  *
162  * "Dot version" names are used in the class loading machinery.
163  */
164 char* dvmDotToDescriptor(const char* str);
165
166 /*
167  * Return a newly-allocated string for the internal-form class name for
168  * the given type descriptor. That is, the initial "L" and final ";" (if
169  * any) have been removed.
170  */
171 char* dvmDescriptorToName(const char* str);
172
173 /*
174  * Return a newly-allocated string for the type descriptor for the given
175  * internal-form class name. That is, a non-array class name will get
176  * surrounded by "L" and ";", while array names are left as-is.
177  */
178 char* dvmNameToDescriptor(const char* str);
179
180 /*
181  * Get the current time, in nanoseconds.  This is "relative" time, meaning
182  * it could be wall-clock time or a monotonic counter, and is only suitable
183  * for computing time deltas.
184  */
185 u8 dvmGetRelativeTimeNsec(void);
186
187 /*
188  * Get the current time, in microseconds.  This is "relative" time, meaning
189  * it could be wall-clock time or a monotonic counter, and is only suitable
190  * for computing time deltas.
191  */
192 INLINE u8 dvmGetRelativeTimeUsec(void) {
193     return dvmGetRelativeTimeNsec() / 1000;
194 }
195
196 /*
197  * Get the current time, in milliseconds.  This is "relative" time,
198  * meaning it could be wall-clock time or a monotonic counter, and is
199  * only suitable for computing time deltas.  The value returned from
200  * this function is a u4 and should only be used for debugging
201  * messages.  TODO: make this value relative to the start-up time of
202  * the VM.
203  */
204 INLINE u4 dvmGetRelativeTimeMsec(void) {
205     return (u4)(dvmGetRelativeTimeUsec() / 1000);
206 }
207
208 /*
209  * Get the current per-thread CPU time.  This clock increases monotonically
210  * when the thread is running, but not when it's sleeping or blocked on a
211  * synchronization object.
212  *
213  * The absolute value of the clock may not be useful, so this should only
214  * be used for time deltas.
215  *
216  * If the thread CPU clock is not available, this always returns (u8)-1.
217  */
218 u8 dvmGetThreadCpuTimeNsec(void);
219
220 /*
221  * Per-thread CPU time, in micros.
222  */
223 INLINE u8 dvmGetThreadCpuTimeUsec(void) {
224     return dvmGetThreadCpuTimeNsec() / 1000;
225 }
226
227 /*
228  * Like dvmGetThreadCpuTimeNsec, but for a different thread.
229  */
230 u8 dvmGetOtherThreadCpuTimeNsec(pthread_t thread);
231 INLINE u8 dvmGetOtherThreadCpuTimeUsec(pthread_t thread) {
232     return dvmGetOtherThreadCpuTimeNsec(thread) / 1000;
233 }
234
235 /*
236  * Sleep for increasingly longer periods, until "maxTotalSleep" microseconds
237  * have elapsed.  Pass in the start time, which must be a value returned by
238  * dvmGetRelativeTimeUsec().
239  *
240  * Returns "false" if we were unable to sleep because our time is up.
241  */
242 bool dvmIterativeSleep(int iteration, int maxTotalSleep, u8 relStartTime);
243
244 /*
245  * Set the "close on exec" flag on a file descriptor.
246  */
247 bool dvmSetCloseOnExec(int fd);
248
249 /*
250  * Unconditionally abort the entire VM.  Try not to use this.
251  *
252  * NOTE: if this is marked ((noreturn)), gcc will merge multiple dvmAbort()
253  * calls in a single function together.  This is good, in that it reduces
254  * code size slightly, but also bad, because the native stack trace we
255  * get from the abort may point at the wrong call site.  Best to leave
256  * it undecorated.
257  */
258 void dvmAbort(void);
259 void dvmPrintNativeBackTrace(void);
260
261 #if (!HAVE_STRLCPY)
262 /* Implementation of strlcpy() for platforms that don't already have it. */
263 size_t strlcpy(char *dst, const char *src, size_t size);
264 #endif
265
266 /*
267  *  Allocates a memory region using ashmem and mmap, initialized to
268  *  zero.  Actual allocation rounded up to page multiple.  Returns
269  *  NULL on failure.
270  */
271 void *dvmAllocRegion(size_t size, int prot, const char *name);
272
273 /*
274  * Get some per-thread stats from /proc/self/task/N/stat.
275  */
276 typedef struct {
277     unsigned long utime;    /* number of jiffies scheduled in user mode */
278     unsigned long stime;    /* number of jiffies scheduled in kernel mode */
279     int processor;          /* number of CPU that last executed thread */
280 } ProcStatData;
281 bool dvmGetThreadStats(ProcStatData* pData, pid_t tid);
282
283 /*
284  * Returns the pointer to the "absolute path" part of the given path
285  * string, treating first (if any) instance of "/./" as a sentinel
286  * indicating the start of the absolute path. If the path isn't absolute
287  * in the usual way (i.e., starts with "/") and doesn't have the sentinel,
288  * then this returns NULL.
289  *
290  * For example:
291  *     "/foo/bar/baz" returns "/foo/bar/baz"
292  *     "foo/./bar/baz" returns "/bar/baz"
293  *     "foo/bar/baz" returns NULL
294  *
295  * The sentinel is used specifically to aid in cross-optimization, where
296  * a host is processing dex files in a build tree, and where we don't want
297  * the build tree's directory structure to be baked into the output (such
298  * as, for example, in the dependency paths of optimized dex files).
299  */
300 const char* dvmPathToAbsolutePortion(const char* path);
301
302 #endif /*_DALVIK_MISC*/