YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/os-util.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
// Imported from Impala. Changes include:
33
// - Namespace + imports.
34
// - Fixes for cpplint.
35
// - Fixed parsing when thread names have spaces.
36
37
#ifndef YB_UTIL_OS_UTIL_H
38
#define YB_UTIL_OS_UTIL_H
39
40
#include <string>
41
42
#include "yb/util/status_fwd.h"
43
44
namespace yb {
45
46
// Utility methods to read interesting values from /proc.
47
// TODO: Get stats for parent process.
48
49
// Container struct for statistics read from the /proc filesystem for a thread.
50
struct ThreadStats {
51
  int64_t user_ns;
52
  int64_t kernel_ns;
53
  int64_t iowait_ns;
54
55
  // Default constructor zeroes all members in case structure can't be filled by
56
  // GetThreadStats.
57
3
  ThreadStats() : user_ns(0), kernel_ns(0), iowait_ns(0) { }
58
};
59
60
// Populates ThreadStats object using a given buffer. The buffer is expected to
61
// conform to /proc/<pid>/task/<tid>/stat layout; an error will be returned otherwise.
62
//
63
// If 'name' is supplied, the extracted thread name will be written to it.
64
Status ParseStat(const std::string&buffer, std::string* name, ThreadStats* stats);
65
66
// Populates ThreadStats object for a given thread by reading from
67
// /proc/<pid>/task/<tid>/stat. Returns OK unless the file cannot be read or is in an
68
// unrecognised format, or if the kernel version is not modern enough.
69
Status GetThreadStats(int64_t tid, ThreadStats* stats);
70
71
// Runs a shell command. Returns false if there was any error (either failure to launch or
72
// non-0 exit code), and true otherwise. *msg is set to an error message including the OS
73
// error string, if any, and the first 1k of output if there was any error, or just the
74
// first 1k of output otherwise.
75
bool RunShellProcess(const std::string& cmd, std::string* msg);
76
77
} // namespace yb
78
79
#endif /* YB_UTIL_OS_UTIL_H */