xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingUtil.h (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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 /* PS4 doesn't have setenv/getenv/fork. Define a shim. */
35 #if __ORBIS__
36 #include <sys/types.h>
37 static inline char *getenv(const char *name) { return NULL; }
38 static inline int setenv(const char *name, const char *value, int overwrite)
39 { return 0; }
40 static pid_t fork() { return -1; }
41 #endif /* #if __ORBIS__ */
42 
43 /* GCOV_PREFIX and GCOV_PREFIX_STRIP support */
44 /* Return the path prefix specified by GCOV_PREFIX environment variable.
45  * If GCOV_PREFIX_STRIP is also specified, the strip level (integer value)
46  * is returned via \c *PrefixStrip. The prefix length is stored in *PrefixLen.
47  */
48 const char *lprofGetPathPrefix(int *PrefixStrip, size_t *PrefixLen);
49 /* Apply the path prefix specified in \c Prefix to path string in \c PathStr,
50  * and store the result to buffer pointed to by \c Buffer. If \c PrefixStrip
51  * is not zero, path prefixes are stripped from \c PathStr (the level of
52  * stripping is specified by \c PrefixStrip) before \c Prefix is added.
53  */
54 void lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
55                           size_t PrefixLen, int PrefixStrip);
56 
57 /* Returns a pointer to the first occurrence of \c DIR_SEPARATOR char in
58  * the string \c Path, or NULL if the char is not found. */
59 const char *lprofFindFirstDirSeparator(const char *Path);
60 /* Returns a pointer to the last occurrence of \c DIR_SEPARATOR char in
61  * the string \c Path, or NULL if the char is not found. */
62 const char *lprofFindLastDirSeparator(const char *Path);
63 
64 int lprofGetHostName(char *Name, int Len);
65 
66 unsigned lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV);
67 void *lprofPtrFetchAdd(void **Mem, long ByteIncr);
68 
69 /* Temporarily suspend SIGKILL. Return value of 1 means a restore is needed.
70  * Other return values mean no restore is needed.
71  */
72 int lprofSuspendSigKill();
73 
74 /* Restore previously suspended SIGKILL. */
75 void lprofRestoreSigKill();
76 
77 static inline size_t lprofRoundUpTo(size_t x, size_t boundary) {
78   return (x + boundary - 1) & ~(boundary - 1);
79 }
80 
81 static inline size_t lprofRoundDownTo(size_t x, size_t boundary) {
82   return x & ~(boundary - 1);
83 }
84 
85 int lprofReleaseMemoryPagesToOS(uintptr_t Begin, uintptr_t End);
86 
87 #endif /* PROFILE_INSTRPROFILINGUTIL_H */
88