10b57cec5SDimitry Andric /*===- InstrProfilingUtil.h - Support library for PGO instrumentation -----===*\
20b57cec5SDimitry Andric |*
30b57cec5SDimitry Andric |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric |* See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric |*
70b57cec5SDimitry Andric \*===----------------------------------------------------------------------===*/
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #ifndef PROFILE_INSTRPROFILINGUTIL_H
100b57cec5SDimitry Andric #define PROFILE_INSTRPROFILINGUTIL_H
110b57cec5SDimitry Andric
12*fe6060f1SDimitry Andric #include <inttypes.h>
130b57cec5SDimitry Andric #include <stddef.h>
140b57cec5SDimitry Andric #include <stdio.h>
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric /*! \brief Create a directory tree. */
170b57cec5SDimitry Andric void __llvm_profile_recursive_mkdir(char *Pathname);
180b57cec5SDimitry Andric
190b57cec5SDimitry Andric /*! Set the mode used when creating profile directories. */
200b57cec5SDimitry Andric void __llvm_profile_set_dir_mode(unsigned Mode);
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric /*! Return the directory creation mode. */
230b57cec5SDimitry Andric unsigned __llvm_profile_get_dir_mode(void);
240b57cec5SDimitry Andric
250b57cec5SDimitry Andric int lprofLockFd(int fd);
260b57cec5SDimitry Andric int lprofUnlockFd(int fd);
270b57cec5SDimitry Andric int lprofLockFileHandle(FILE *F);
280b57cec5SDimitry Andric int lprofUnlockFileHandle(FILE *F);
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric /*! Open file \c Filename for read+write with write
310b57cec5SDimitry Andric * lock for exclusive access. The caller will block
320b57cec5SDimitry Andric * if the lock is already held by another process. */
330b57cec5SDimitry Andric FILE *lprofOpenFileEx(const char *Filename);
345ffd83dbSDimitry Andric /* PS4 doesn't have setenv/getenv/fork. Define a shim. */
350b57cec5SDimitry Andric #if __ORBIS__
365ffd83dbSDimitry Andric #include <sys/types.h>
getenv(const char * name)370b57cec5SDimitry Andric static inline char *getenv(const char *name) { return NULL; }
setenv(const char * name,const char * value,int overwrite)38480093f4SDimitry Andric static inline int setenv(const char *name, const char *value, int overwrite)
39480093f4SDimitry Andric { return 0; }
fork()405ffd83dbSDimitry Andric static pid_t fork() { return -1; }
410b57cec5SDimitry Andric #endif /* #if __ORBIS__ */
420b57cec5SDimitry Andric
430b57cec5SDimitry Andric /* GCOV_PREFIX and GCOV_PREFIX_STRIP support */
440b57cec5SDimitry Andric /* Return the path prefix specified by GCOV_PREFIX environment variable.
450b57cec5SDimitry Andric * If GCOV_PREFIX_STRIP is also specified, the strip level (integer value)
460b57cec5SDimitry Andric * is returned via \c *PrefixStrip. The prefix length is stored in *PrefixLen.
470b57cec5SDimitry Andric */
480b57cec5SDimitry Andric const char *lprofGetPathPrefix(int *PrefixStrip, size_t *PrefixLen);
490b57cec5SDimitry Andric /* Apply the path prefix specified in \c Prefix to path string in \c PathStr,
500b57cec5SDimitry Andric * and store the result to buffer pointed to by \c Buffer. If \c PrefixStrip
510b57cec5SDimitry Andric * is not zero, path prefixes are stripped from \c PathStr (the level of
520b57cec5SDimitry Andric * stripping is specified by \c PrefixStrip) before \c Prefix is added.
530b57cec5SDimitry Andric */
540b57cec5SDimitry Andric void lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
550b57cec5SDimitry Andric size_t PrefixLen, int PrefixStrip);
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric /* Returns a pointer to the first occurrence of \c DIR_SEPARATOR char in
580b57cec5SDimitry Andric * the string \c Path, or NULL if the char is not found. */
590b57cec5SDimitry Andric const char *lprofFindFirstDirSeparator(const char *Path);
600b57cec5SDimitry Andric /* Returns a pointer to the last occurrence of \c DIR_SEPARATOR char in
610b57cec5SDimitry Andric * the string \c Path, or NULL if the char is not found. */
620b57cec5SDimitry Andric const char *lprofFindLastDirSeparator(const char *Path);
630b57cec5SDimitry Andric
640b57cec5SDimitry Andric int lprofGetHostName(char *Name, int Len);
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric unsigned lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV);
670b57cec5SDimitry Andric void *lprofPtrFetchAdd(void **Mem, long ByteIncr);
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric /* Temporarily suspend SIGKILL. Return value of 1 means a restore is needed.
700b57cec5SDimitry Andric * Other return values mean no restore is needed.
710b57cec5SDimitry Andric */
720b57cec5SDimitry Andric int lprofSuspendSigKill();
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric /* Restore previously suspended SIGKILL. */
750b57cec5SDimitry Andric void lprofRestoreSigKill();
760b57cec5SDimitry Andric
lprofRoundUpTo(size_t x,size_t boundary)77*fe6060f1SDimitry Andric static inline size_t lprofRoundUpTo(size_t x, size_t boundary) {
78*fe6060f1SDimitry Andric return (x + boundary - 1) & ~(boundary - 1);
79*fe6060f1SDimitry Andric }
80*fe6060f1SDimitry Andric
lprofRoundDownTo(size_t x,size_t boundary)81*fe6060f1SDimitry Andric static inline size_t lprofRoundDownTo(size_t x, size_t boundary) {
82*fe6060f1SDimitry Andric return x & ~(boundary - 1);
83*fe6060f1SDimitry Andric }
84*fe6060f1SDimitry Andric
85*fe6060f1SDimitry Andric int lprofReleaseMemoryPagesToOS(uintptr_t Begin, uintptr_t End);
86*fe6060f1SDimitry Andric
870b57cec5SDimitry Andric #endif /* PROFILE_INSTRPROFILINGUTIL_H */
88