13cab2bb3Spatrick /*===- InstrProfilingUtil.h - Support library for PGO instrumentation -----===*\
23cab2bb3Spatrick |*
33cab2bb3Spatrick |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick |* See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick |*
73cab2bb3Spatrick \*===----------------------------------------------------------------------===*/
83cab2bb3Spatrick
93cab2bb3Spatrick #ifndef PROFILE_INSTRPROFILINGUTIL_H
103cab2bb3Spatrick #define PROFILE_INSTRPROFILINGUTIL_H
113cab2bb3Spatrick
12*d89ec533Spatrick #include <inttypes.h>
133cab2bb3Spatrick #include <stddef.h>
143cab2bb3Spatrick #include <stdio.h>
153cab2bb3Spatrick
163cab2bb3Spatrick /*! \brief Create a directory tree. */
173cab2bb3Spatrick void __llvm_profile_recursive_mkdir(char *Pathname);
183cab2bb3Spatrick
193cab2bb3Spatrick /*! Set the mode used when creating profile directories. */
203cab2bb3Spatrick void __llvm_profile_set_dir_mode(unsigned Mode);
213cab2bb3Spatrick
223cab2bb3Spatrick /*! Return the directory creation mode. */
233cab2bb3Spatrick unsigned __llvm_profile_get_dir_mode(void);
243cab2bb3Spatrick
253cab2bb3Spatrick int lprofLockFd(int fd);
263cab2bb3Spatrick int lprofUnlockFd(int fd);
273cab2bb3Spatrick int lprofLockFileHandle(FILE *F);
283cab2bb3Spatrick int lprofUnlockFileHandle(FILE *F);
293cab2bb3Spatrick
303cab2bb3Spatrick /*! Open file \c Filename for read+write with write
313cab2bb3Spatrick * lock for exclusive access. The caller will block
323cab2bb3Spatrick * if the lock is already held by another process. */
333cab2bb3Spatrick FILE *lprofOpenFileEx(const char *Filename);
341f9cb04fSpatrick /* PS4 doesn't have setenv/getenv/fork. Define a shim. */
353cab2bb3Spatrick #if __ORBIS__
361f9cb04fSpatrick #include <sys/types.h>
getenv(const char * name)373cab2bb3Spatrick static inline char *getenv(const char *name) { return NULL; }
setenv(const char * name,const char * value,int overwrite)383cab2bb3Spatrick static inline int setenv(const char *name, const char *value, int overwrite)
393cab2bb3Spatrick { return 0; }
fork()401f9cb04fSpatrick static pid_t fork() { return -1; }
413cab2bb3Spatrick #endif /* #if __ORBIS__ */
423cab2bb3Spatrick
433cab2bb3Spatrick /* GCOV_PREFIX and GCOV_PREFIX_STRIP support */
443cab2bb3Spatrick /* Return the path prefix specified by GCOV_PREFIX environment variable.
453cab2bb3Spatrick * If GCOV_PREFIX_STRIP is also specified, the strip level (integer value)
463cab2bb3Spatrick * is returned via \c *PrefixStrip. The prefix length is stored in *PrefixLen.
473cab2bb3Spatrick */
483cab2bb3Spatrick const char *lprofGetPathPrefix(int *PrefixStrip, size_t *PrefixLen);
493cab2bb3Spatrick /* Apply the path prefix specified in \c Prefix to path string in \c PathStr,
503cab2bb3Spatrick * and store the result to buffer pointed to by \c Buffer. If \c PrefixStrip
513cab2bb3Spatrick * is not zero, path prefixes are stripped from \c PathStr (the level of
523cab2bb3Spatrick * stripping is specified by \c PrefixStrip) before \c Prefix is added.
533cab2bb3Spatrick */
543cab2bb3Spatrick void lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
553cab2bb3Spatrick size_t PrefixLen, int PrefixStrip);
563cab2bb3Spatrick
573cab2bb3Spatrick /* Returns a pointer to the first occurrence of \c DIR_SEPARATOR char in
583cab2bb3Spatrick * the string \c Path, or NULL if the char is not found. */
593cab2bb3Spatrick const char *lprofFindFirstDirSeparator(const char *Path);
603cab2bb3Spatrick /* Returns a pointer to the last occurrence of \c DIR_SEPARATOR char in
613cab2bb3Spatrick * the string \c Path, or NULL if the char is not found. */
623cab2bb3Spatrick const char *lprofFindLastDirSeparator(const char *Path);
633cab2bb3Spatrick
643cab2bb3Spatrick int lprofGetHostName(char *Name, int Len);
653cab2bb3Spatrick
663cab2bb3Spatrick unsigned lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV);
673cab2bb3Spatrick void *lprofPtrFetchAdd(void **Mem, long ByteIncr);
683cab2bb3Spatrick
693cab2bb3Spatrick /* Temporarily suspend SIGKILL. Return value of 1 means a restore is needed.
703cab2bb3Spatrick * Other return values mean no restore is needed.
713cab2bb3Spatrick */
723cab2bb3Spatrick int lprofSuspendSigKill();
733cab2bb3Spatrick
743cab2bb3Spatrick /* Restore previously suspended SIGKILL. */
753cab2bb3Spatrick void lprofRestoreSigKill();
763cab2bb3Spatrick
lprofRoundUpTo(size_t x,size_t boundary)77*d89ec533Spatrick static inline size_t lprofRoundUpTo(size_t x, size_t boundary) {
78*d89ec533Spatrick return (x + boundary - 1) & ~(boundary - 1);
79*d89ec533Spatrick }
80*d89ec533Spatrick
lprofRoundDownTo(size_t x,size_t boundary)81*d89ec533Spatrick static inline size_t lprofRoundDownTo(size_t x, size_t boundary) {
82*d89ec533Spatrick return x & ~(boundary - 1);
83*d89ec533Spatrick }
84*d89ec533Spatrick
85*d89ec533Spatrick int lprofReleaseMemoryPagesToOS(uintptr_t Begin, uintptr_t End);
86*d89ec533Spatrick
873cab2bb3Spatrick #endif /* PROFILE_INSTRPROFILINGUTIL_H */
88