LLVM 24.0.0git
Unix.h
Go to the documentation of this file.
1//===- llvm/Support/Unix/Unix.h - Common Unix Include File -------*- C++ -*-===//
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// This file defines things specific to Unix implementations.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_SUPPORT_UNIX_UNIX_H
14#define LLVM_LIB_SUPPORT_UNIX_UNIX_H
15
16//===----------------------------------------------------------------------===//
17//=== WARNING: Implementation here must contain only generic UNIX code that
18//=== is guaranteed to work on all UNIX variants.
19//===----------------------------------------------------------------------===//
20
21#include "llvm/Config/config.h"
22#include "llvm/Support/Chrono.h"
23#include "llvm/Support/Errno.h"
26#include <assert.h>
27#include <cerrno>
28#include <cstdio>
29#include <cstdlib>
30#include <cstring>
31#include <string>
32#include <sys/resource.h>
33#include <sys/types.h>
34#include <sys/wait.h>
35
36#ifdef HAVE_UNISTD_H
37#include <unistd.h>
38#endif
39
40#include <sys/time.h>
41#include <time.h>
42
43#include <dlfcn.h>
44
45# include <fcntl.h>
46
47/// This function builds an error message into \p ErrMsg using the \p prefix
48/// string and the Unix error number given by \p errnum. If errnum is -1, the
49/// default then the value of errno is used.
50/// Make an error message
51///
52/// If the error number can be converted to a string, it will be
53/// separated from prefix by ": ".
54static inline bool MakeErrMsg(
55 std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
56 if (!ErrMsg)
57 return true;
58 if (errnum == -1)
59 errnum = errno;
60 *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
61 return true;
62}
63
64// Include StrError(errnum) in a fatal error message.
65[[noreturn]] static inline void ReportErrnumFatal(const char *Msg, int errnum) {
66 std::string ErrMsg;
67 MakeErrMsg(&ErrMsg, Msg, errnum);
69}
70
71namespace llvm {
72namespace sys {
73
74/// Convert a struct timeval to a duration. Note that timeval can be used both
75/// as a time point and a duration. Be sure to check what the input represents.
76inline std::chrono::microseconds toDuration(const struct timeval &TV) {
77 return std::chrono::seconds(TV.tv_sec) +
78 std::chrono::microseconds(TV.tv_usec);
79}
80
81/// Convert resource usage of this process or a waited child to statistics.
82inline ProcessStatistics toProcessStatistics(const struct rusage &RU) {
83 std::chrono::microseconds UserT = toDuration(RU.ru_utime);
84 std::chrono::microseconds KernelT = toDuration(RU.ru_stime);
85 uint64_t PeakMemory = 0;
86#if !defined(__HAIKU__) && !defined(__MVS__)
87 PeakMemory = static_cast<uint64_t>(RU.ru_maxrss);
88#if defined(__APPLE__)
89 PeakMemory /= 1024; // ru_maxrss is in bytes on Darwin, KiB elsewhere.
90#endif
91#endif
92 return ProcessStatistics{UserT + KernelT, UserT, PeakMemory};
93}
94
95/// Convert a time point to struct timespec.
96inline struct timespec toTimeSpec(TimePoint<> TP) {
97 using namespace std::chrono;
98
99 struct timespec RetVal;
100 RetVal.tv_sec = toTimeT(TP);
101 RetVal.tv_nsec = (TP.time_since_epoch() % seconds(1)).count();
102 return RetVal;
103}
104
105/// Convert a time point to struct timeval.
106inline struct timeval toTimeVal(TimePoint<std::chrono::microseconds> TP) {
107 using namespace std::chrono;
108
109 struct timeval RetVal;
110 RetVal.tv_sec = toTimeT(TP);
111 RetVal.tv_usec = (TP.time_since_epoch() % seconds(1)).count();
112 return RetVal;
113}
114
115} // namespace sys
116} // namespace llvm
117
118#endif
uint64_t uint64_t
const char * Msg
static bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix, int errnum=-1)
This function builds an error message into ErrMsg using the prefix string and the Unix error number g...
Definition Unix.h:54
static void ReportErrnumFatal(const char *Msg, int errnum)
Definition Unix.h:65
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
std::chrono::nanoseconds toDuration(FILETIME Time)
LLVM_ABI std::string StrError()
Returns a string representation of the errno value, using whatever thread-safe variant of strerror() ...
Definition Errno.cpp:26
ProcessStatistics toProcessStatistics(const struct rusage &RU)
Convert resource usage of this process or a waited child to statistics.
Definition Unix.h:82
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition Chrono.h:34
struct timespec toTimeSpec(TimePoint<> TP)
Convert a time point to struct timespec.
Definition Unix.h:96
struct timeval toTimeVal(TimePoint< std::chrono::microseconds > TP)
Convert a time point to struct timeval.
Definition Unix.h:106
std::time_t toTimeT(TimePoint<> TP)
Convert a TimePoint to std::time_t.
Definition Chrono.h:50
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879
This struct encapsulates information about a process execution.
Definition Program.h:60