Misc.h revision fe7f2b3920bf5d66eda262e643245b03df3e57c8
13ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
23ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Copyright (C) 2008 The Android Open Source Project
33ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
43ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
53ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * you may not use this file except in compliance with the License.
63ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * You may obtain a copy of the License at
73ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
83ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
93ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
103ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Unless required by applicable law or agreed to in writing, software
113ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
123ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * See the License for the specific language governing permissions and
143ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * limitations under the License.
153ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
163ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
173ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
183ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Miscellaneous utility functions.
193ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
203ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#ifndef DALVIK_MISC_H_
213ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#define DALVIK_MISC_H_
223ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
233ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#include <string>
243ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
253ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#include <stdarg.h>
263ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#include <stdio.h>
273ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#include <sys/types.h>
283ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#include <sys/time.h>
293ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
303ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#include "Inlines.h"
313ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
323ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
333ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Used to shut up the compiler when a parameter isn't used.
343ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
353ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#define UNUSED_PARAMETER(p)     (void)(p)
363ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
373ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
383ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Floating point conversion functions.  These are necessary to avoid
393ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * strict-aliasing problems ("dereferencing type-punned pointer will break
403ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * strict-aliasing rules").  According to the gcc info page, this usage
413ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * is allowed, even with "-fstrict-aliasing".
423ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
433ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * The code generated by gcc-4.1.1 appears to be much better than a
443ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * type cast dereference ("int foo = *(int*)&myfloat") when the conversion
453ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * function is inlined.  It also allows us to take advantage of the
463ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * optimizations that strict aliasing rules allow.
473ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
483ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe OnoratoINLINE float dvmU4ToFloat(u4 val) {
493ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    union { u4 in; float out; } conv;
503ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    conv.in = val;
513ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    return conv.out;
523ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato}
533ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe OnoratoINLINE u4 dvmFloatToU4(float val) {
543ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    union { float in; u4 out; } conv;
553ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    conv.in = val;
563ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    return conv.out;
573ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato}
583ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
593ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
603ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Print a hex dump to the log file.
613ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
623ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * "local" mode prints a hex dump starting from offset 0 (roughly equivalent
633ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * to "xxd -g1").
643ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
653ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * "mem" mode shows the actual memory address, and will offset the start
663ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * so that the low nibble of the address is always zero.
673ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
683ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * If "tag" is NULL the default tag ("dalvikvm") will be used.
693ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
703ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratoenum HexDumpMode { kHexDumpLocal, kHexDumpMem };
713ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratovoid dvmPrintHexDumpEx(int priority, const char* tag, const void* vaddr,
723ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    size_t length, HexDumpMode mode);
733ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
743ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
753ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Print a hex dump, at INFO level.
763ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
773ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe OnoratoINLINE void dvmPrintHexDump(const void* vaddr, size_t length) {
783ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    dvmPrintHexDumpEx(ANDROID_LOG_INFO, LOG_TAG,
793ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        vaddr, length, kHexDumpLocal);
803ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato}
813ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
823ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
833ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Print a hex dump at VERBOSE level. This does nothing in non-debug builds.
843ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
853ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe OnoratoINLINE void dvmPrintHexDumpDbg(const void* vaddr, size_t length,const char* tag)
863ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato{
873ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#if !LOG_NDEBUG
883ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    dvmPrintHexDumpEx(ANDROID_LOG_VERBOSE, (tag != NULL) ? tag : LOG_TAG,
893ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        vaddr, length, kHexDumpLocal);
903ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#endif
913ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato}
923ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
933ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratoenum DebugTargetKind {
943ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    kDebugTargetUnknown = 0,
953ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    kDebugTargetLog,
963ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    kDebugTargetFile,
973ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato};
983ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
993ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
1003ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * We pass one of these around when we want code to be able to write debug
1013ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * info to either the log or to a file (or stdout/stderr).
1023ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
1033ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratostruct DebugOutputTarget {
1043ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    /* where to? */
1053ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    DebugTargetKind which;
1063ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1073ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    /* additional bits */
1083ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    union {
1093ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        struct {
1103ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            int priority;
1113ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            const char* tag;
1123ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        } log;
1133ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        struct {
1143ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato            FILE* fp;
1153ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato        } file;
1163ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    } data;
1173ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato};
1183ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1193ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
1203ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Fill in a DebugOutputTarget struct.
1213ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
1223ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratovoid dvmCreateLogOutputTarget(DebugOutputTarget* target, int priority,
1233ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    const char* tag);
1243ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratovoid dvmCreateFileOutputTarget(DebugOutputTarget* target, FILE* fp);
1253ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1263ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
1273ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Print a debug message.
1283ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
1293ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratovoid dvmPrintDebugMessage(const DebugOutputTarget* target, const char* format,
1303ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    ...)
1313ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#if defined(__GNUC__)
1323ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    __attribute__ ((format(printf, 2, 3)))
1333ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#endif
1343ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    ;
1353ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1363ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
1373ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Return a newly-allocated string in which all occurrences of '.' have
1383ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * been changed to '/'.  If we find a '/' in the original string, NULL
1393ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * is returned to avoid ambiguity.
1403ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
1413ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratochar* dvmDotToSlash(const char* str);
1423ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1433ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
1443ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Return a newly-allocated string containing a human-readable equivalent
1453ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * of 'descriptor'. So "I" would be "int", "[[I" would be "int[][]",
1463ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * "[Ljava/lang/String;" would be "java.lang.String[]", and so forth.
1473ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
1483ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratostd::string dvmHumanReadableDescriptor(const char* descriptor);
1493ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1503ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/**
1513ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Returns a human-readable string form of the name of the class of
1523ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * the given object. So given a java.lang.String, the output would
1533ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * be "java.lang.String". Given an array of int, the output would be "int[]".
1543ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Given String.class, the output would be "java.lang.Class<java.lang.String>".
1553ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
1563ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratostd::string dvmHumanReadableType(const Object* obj);
1573ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1583ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
1593ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Return a newly-allocated string for the "dot version" of the class
1603ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * name for the given type descriptor. That is, The initial "L" and
1613ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * final ";" (if any) have been removed and all occurrences of '/'
1623ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * have been changed to '.'.
1633ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
1643ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * "Dot version" names are used in the class loading machinery.
1653ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * See also dvmHumanReadableDescriptor.
1663ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
1673ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratochar* dvmDescriptorToDot(const char* str);
1683ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1693ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
1703ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Return a newly-allocated string for the type descriptor
1713ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * corresponding to the "dot version" of the given class name. That
1723ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * is, non-array names are surrounded by "L" and ";", and all
1733ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * occurrences of '.' have been changed to '/'.
1743ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
1753ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * "Dot version" names are used in the class loading machinery.
1763ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
1773ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratochar* dvmDotToDescriptor(const char* str);
1783ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1793ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
1803ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Return a newly-allocated string for the internal-form class name for
1813ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * the given type descriptor. That is, the initial "L" and final ";" (if
1823ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * any) have been removed.
1833ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
1843ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratochar* dvmDescriptorToName(const char* str);
1853ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1863ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
1873ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Return a newly-allocated string for the type descriptor for the given
1883ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * internal-form class name. That is, a non-array class name will get
1893ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * surrounded by "L" and ";", while array names are left as-is.
1903ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
1913ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratochar* dvmNameToDescriptor(const char* str);
1923ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1933ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
1943ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Get the current time, in nanoseconds.  This is "relative" time, meaning
1953ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * it could be wall-clock time or a monotonic counter, and is only suitable
1963ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * for computing time deltas.
1973ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
1983ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratou8 dvmGetRelativeTimeNsec(void);
1993ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
2003ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
2013ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Get the current time, in microseconds.  This is "relative" time, meaning
2023ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * it could be wall-clock time or a monotonic counter, and is only suitable
2033ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * for computing time deltas.
2043ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
2053ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe OnoratoINLINE u8 dvmGetRelativeTimeUsec(void) {
2063ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    return dvmGetRelativeTimeNsec() / 1000;
2073ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato}
2083ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
2093ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
2103ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Get the current time, in milliseconds.  This is "relative" time,
2113ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * meaning it could be wall-clock time or a monotonic counter, and is
2123ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * only suitable for computing time deltas.  The value returned from
2133ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * this function is a u4 and should only be used for debugging
2143ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * messages.  TODO: make this value relative to the start-up time of
2153ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * the VM.
2163ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
2173ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe OnoratoINLINE u4 dvmGetRelativeTimeMsec(void) {
2183ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    return (u4)(dvmGetRelativeTimeUsec() / 1000);
2193ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato}
2203ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
2213ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
2223ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Get the current per-thread CPU time.  This clock increases monotonically
2233ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * when the thread is running, but not when it's sleeping or blocked on a
2243ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * synchronization object.
2253ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
2263ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * The absolute value of the clock may not be useful, so this should only
2273ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * be used for time deltas.
2283ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
2293ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * If the thread CPU clock is not available, this always returns (u8)-1.
2303ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
2313ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratou8 dvmGetThreadCpuTimeNsec(void);
2323ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
2333ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
2343ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Per-thread CPU time, in micros.
2353ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
2363ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe OnoratoINLINE u8 dvmGetThreadCpuTimeUsec(void) {
2373ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    return dvmGetThreadCpuTimeNsec() / 1000;
2383ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato}
2393ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
2403ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
2413ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Like dvmGetThreadCpuTimeNsec, but for a different thread.
2423ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
2433ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratou8 dvmGetOtherThreadCpuTimeNsec(pthread_t thread);
2443ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe OnoratoINLINE u8 dvmGetOtherThreadCpuTimeUsec(pthread_t thread) {
2453ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    return dvmGetOtherThreadCpuTimeNsec(thread) / 1000;
2463ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato}
2473ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
2483ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
2493ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Sleep for increasingly longer periods, until "maxTotalSleep" microseconds
2503ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * have elapsed.  Pass in the start time, which must be a value returned by
2513ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * dvmGetRelativeTimeUsec().
2523ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
2533ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Returns "false" if we were unable to sleep because our time is up.
2543ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
2553ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratobool dvmIterativeSleep(int iteration, int maxTotalSleep, u8 relStartTime);
2563ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
2573ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
2583ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Set the "close on exec" flag on a file descriptor.
2593ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
2603ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratobool dvmSetCloseOnExec(int fd);
2613ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
2623ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
2633ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Unconditionally abort the entire VM.  Try not to use this.
2643ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
2653ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * NOTE: if this is marked ((noreturn)), gcc will merge multiple dvmAbort()
2663ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * calls in a single function together.  This is good, in that it reduces
2673ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * code size slightly, but also bad, because the native stack trace we
2683ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * get from the abort may point at the wrong call site.  Best to leave
2693ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * it undecorated.
2703ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
2713ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratoextern "C" void dvmAbort(void);
2723ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratovoid dvmPrintNativeBackTrace(void);
2733ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
2743ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#if (!HAVE_STRLCPY)
2753ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/* Implementation of strlcpy() for platforms that don't already have it. */
2763ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratoextern "C" size_t strlcpy(char *dst, const char *src, size_t size);
2773ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#endif
2783ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
2793ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
2803ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *  Allocates a memory region using ashmem and mmap, initialized to
2813ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *  zero.  Actual allocation rounded up to page multiple.  Returns
2823ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *  NULL on failure.
2833ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
2843ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratovoid *dvmAllocRegion(size_t size, int prot, const char *name);
2853ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
2863ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
2873ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Get some per-thread stats from /proc/self/task/N/stat.
2883ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
2893ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratostruct ProcStatData {
2903ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    unsigned long utime;    /* number of jiffies scheduled in user mode */
2913ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    unsigned long stime;    /* number of jiffies scheduled in kernel mode */
2923ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato    int processor;          /* number of CPU that last executed thread */
2933ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato};
2943ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratobool dvmGetThreadStats(ProcStatData* pData, pid_t tid);
2953ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
2963ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/*
2973ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Returns the pointer to the "absolute path" part of the given path
2983ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * string, treating first (if any) instance of "/./" as a sentinel
2993ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * indicating the start of the absolute path. If the path isn't absolute
3003ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * in the usual way (i.e., starts with "/") and doesn't have the sentinel,
3013ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * then this returns NULL.
3023ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
3033ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * For example:
3043ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *     "/foo/bar/baz" returns "/foo/bar/baz"
3053ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *     "foo/./bar/baz" returns "/bar/baz"
3063ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *     "foo/bar/baz" returns NULL
3073ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato *
3083ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * The sentinel is used specifically to aid in cross-optimization, where
3093ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * a host is processing dex files in a build tree, and where we don't want
3103ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * the build tree's directory structure to be baked into the output (such
3113ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * as, for example, in the dependency paths of optimized dex files).
3123ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
3133ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratoconst char* dvmPathToAbsolutePortion(const char* path);
3143ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
3153ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato/**
3163ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato * Returns a string corresponding to printf-like formatting of the arguments.
3173ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato */
3183ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratostd::string dvmStringPrintf(const char* fmt, ...) __attribute__((__format__ (__printf__, 1, 2)));
3193ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
3203ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#endif  // DALVIK_MISC_H_
3213ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato