1 | /*===- InstrProfilingUtil.h - Support library for PGO instrumentation -----===*\ |
2 | |* |
3 | |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | |* See https://llvm.org/LICENSE.txt for license information. |
5 | |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | |* |
7 | \*===----------------------------------------------------------------------===*/ |
8 | |
9 | #ifndef PROFILE_INSTRPROFILINGUTIL_H |
10 | #define PROFILE_INSTRPROFILINGUTIL_H |
11 | |
12 | #include <inttypes.h> |
13 | #include <stddef.h> |
14 | #include <stdio.h> |
15 | |
16 | /*! \brief Create a directory tree. */ |
17 | void __llvm_profile_recursive_mkdir(char *Pathname); |
18 | |
19 | /*! Set the mode used when creating profile directories. */ |
20 | void __llvm_profile_set_dir_mode(unsigned Mode); |
21 | |
22 | /*! Return the directory creation mode. */ |
23 | unsigned __llvm_profile_get_dir_mode(void); |
24 | |
25 | int lprofLockFd(int fd); |
26 | int lprofUnlockFd(int fd); |
27 | int lprofLockFileHandle(FILE *F); |
28 | int lprofUnlockFileHandle(FILE *F); |
29 | |
30 | /*! Open file \c Filename for read+write with write |
31 | * lock for exclusive access. The caller will block |
32 | * if the lock is already held by another process. */ |
33 | FILE *lprofOpenFileEx(const char *Filename); |
34 | |
35 | enum MemoryStatus { |
36 | MS_INVALID, // Addr is not a valid address |
37 | MS_MMAP, // Addr was mmap'ed |
38 | MS_MALLOC // Addr was malloc'ed |
39 | }; |
40 | typedef struct { |
41 | void *Addr; |
42 | enum MemoryStatus Status; |
43 | } ManagedMemory; |
44 | |
45 | /* Read the content of a file using mmap or fread into a buffer. |
46 | * Certain files (e.g. NFS mounted) cannot be opened reliably with mmap, |
47 | * so we use fread in those cases. The corresponding lprofReleaseBuffer |
48 | * will free/munmap the buffer. |
49 | */ |
50 | void lprofGetFileContentBuffer(FILE *F, uint64_t FileSize, ManagedMemory *Buf); |
51 | void lprofReleaseBuffer(ManagedMemory *FileBuffer, size_t Length); |
52 | |
53 | /* PS4 doesn't have setenv/getenv/fork. Define a shim. */ |
54 | #if __ORBIS__ |
55 | #include <sys/types.h> |
56 | static inline char *getenv(const char *name) { return NULL; } |
57 | static inline int setenv(const char *name, const char *value, int overwrite) |
58 | { return 0; } |
59 | static pid_t fork() { return -1; } |
60 | #endif /* #if __ORBIS__ */ |
61 | |
62 | /* GCOV_PREFIX and GCOV_PREFIX_STRIP support */ |
63 | /* Return the path prefix specified by GCOV_PREFIX environment variable. |
64 | * If GCOV_PREFIX_STRIP is also specified, the strip level (integer value) |
65 | * is returned via \c *PrefixStrip. The prefix length is stored in *PrefixLen. |
66 | */ |
67 | const char *lprofGetPathPrefix(int *PrefixStrip, size_t *PrefixLen); |
68 | /* Apply the path prefix specified in \c Prefix to path string in \c PathStr, |
69 | * and store the result to buffer pointed to by \c Buffer. If \c PrefixStrip |
70 | * is not zero, path prefixes are stripped from \c PathStr (the level of |
71 | * stripping is specified by \c PrefixStrip) before \c Prefix is added. |
72 | */ |
73 | void lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix, |
74 | size_t PrefixLen, int PrefixStrip); |
75 | |
76 | /* Returns a pointer to the first occurrence of \c DIR_SEPARATOR char in |
77 | * the string \c Path, or NULL if the char is not found. */ |
78 | const char *lprofFindFirstDirSeparator(const char *Path); |
79 | /* Returns a pointer to the last occurrence of \c DIR_SEPARATOR char in |
80 | * the string \c Path, or NULL if the char is not found. */ |
81 | const char *lprofFindLastDirSeparator(const char *Path); |
82 | |
83 | int lprofGetHostName(char *Name, int Len); |
84 | |
85 | unsigned lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV); |
86 | void *lprofPtrFetchAdd(void **Mem, long ByteIncr); |
87 | |
88 | /* Temporarily suspend SIGKILL. Return value of 1 means a restore is needed. |
89 | * Other return values mean no restore is needed. |
90 | */ |
91 | int lprofSuspendSigKill(void); |
92 | |
93 | /* Restore previously suspended SIGKILL. */ |
94 | void lprofRestoreSigKill(void); |
95 | |
96 | static inline size_t lprofRoundUpTo(size_t x, size_t boundary) { |
97 | return (x + boundary - 1) & ~(boundary - 1); |
98 | } |
99 | |
100 | static inline size_t lprofRoundDownTo(size_t x, size_t boundary) { |
101 | return x & ~(boundary - 1); |
102 | } |
103 | |
104 | int lprofReleaseMemoryPagesToOS(uintptr_t Begin, uintptr_t End); |
105 | |
106 | typedef void (*AtExit_Fn_ptr)(void); |
107 | |
108 | /* Call atexit and perform other platform-specific bookkeeping. */ |
109 | int lprofAtExit(AtExit_Fn_ptr); |
110 | |
111 | #endif /* PROFILE_INSTRPROFILINGUTIL_H */ |
112 | |