Motr  M0
umisc.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include <stdlib.h> /* strtoull, strtoul */
24 #include <netdb.h> /* gethostbyname_r */
25 #include <arpa/inet.h> /* inet_ntoa, inet_ntop */
26 #include <unistd.h> /* gethostname */
27 #include <errno.h>
28 #include <sys/time.h> /* getrusage */
29 #include <sys/resource.h> /* getrusage */
30 #include <stdio.h> /* fopen */
31 #include "lib/misc.h"
32 
33 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_LIB
34 #include "lib/trace.h"
35 
36 uint64_t m0_strtou64(const char *str, char **endptr, int base)
37 {
38  return strtoull(str, endptr, base);
39 }
40 M0_EXPORTED(m0_strtou64);
41 
42 uint32_t m0_strtou32(const char *str, char **endptr, int base)
43 {
44  return strtoul(str, endptr, base);
45 }
46 M0_EXPORTED(m0_strtou32);
47 
49 M0_INTERNAL int m0_host_resolve(const char *name, char *buf, size_t bufsiz)
50 {
51  int i;
52  int rc = 0;
53  struct in_addr ipaddr;
54 
55  if (inet_aton(name, &ipaddr) == 0) {
56  struct hostent he;
57  char he_buf[4096];
58  struct hostent *hp;
59  int herrno;
60 
61  rc = gethostbyname_r(name, &he, he_buf, sizeof he_buf,
62  &hp, &herrno);
63  if (rc != 0 || hp == NULL)
64  return M0_ERR(-ENOENT);
65  for (i = 0; hp->h_addr_list[i] != NULL; ++i)
66  /* take 1st IPv4 address found */
67  if (hp->h_addrtype == AF_INET &&
68  hp->h_length == sizeof(ipaddr))
69  break;
70  if (hp->h_addr_list[i] == NULL)
71  return M0_ERR(-EPFNOSUPPORT);
72  if (inet_ntop(hp->h_addrtype, hp->h_addr, buf, bufsiz) == NULL)
73  rc = -errno;
74  } else if (strlen(name) >= bufsiz) {
75  rc = -ENOSPC;
76  } else {
77  strcpy(buf, name);
78  }
79  return M0_RC(rc);
80 }
81 
82 M0_INTERNAL void m0_performance_counters(char *buf, size_t buf_len)
83 {
84  struct rusage usage;
85  FILE *proc_self_io;
86  int nr;
87  int rc;
88  int len;
89  char proc_io_buf[0x1000];
90  unsigned long long rchar;
91  unsigned long long wchar;
92  unsigned long long syscr;
93  unsigned long long syscw;
94  unsigned long long read_bytes;
95  unsigned long long write_bytes;
96  unsigned long long cancelled_write_bytes;
97 
98  rc = getrusage(RUSAGE_SELF, &usage);
99  if (rc == 0) {
100  len = snprintf(buf, buf_len, "utime %ld.%06ld stime %ld.%06ld "
101  "maxrss %ld nvcsw %ld nivcsw %ld\n"
102  "minflt %ld majflt %ld "
103  "inblock %ld oublock %ld\n",
104  usage.ru_utime.tv_sec, usage.ru_utime.tv_usec,
105  usage.ru_stime.tv_sec, usage.ru_stime.tv_usec,
106  usage.ru_maxrss, usage.ru_nvcsw, usage.ru_nivcsw,
107  usage.ru_minflt, usage.ru_majflt,
108  usage.ru_inblock, usage.ru_oublock);
109  if (len >= buf_len || len < 0)
110  len = buf_len;
111  buf_len -= len;
112  buf += len;
113  }
114  if (buf_len <= 1)
115  return;
116  proc_self_io = fopen("/proc/self/io", "r");
117  if (proc_self_io != NULL) {
118  nr = fscanf(proc_self_io, "%s %llu %s %llu %s %llu %s %llu "
119  "%s %llu %s %llu %s %llu",
120  proc_io_buf, &rchar, proc_io_buf, &wchar,
121  proc_io_buf, &syscr, proc_io_buf, &syscw,
122  proc_io_buf, &read_bytes, proc_io_buf,
123  &write_bytes, proc_io_buf, &cancelled_write_bytes);
124  fclose(proc_self_io);
125  if (nr == 14) {
126  snprintf(buf, buf_len, "rchar %llu wchar %llu "
127  "syscr %llu syscw %llu\n"
128  "read_bytes %llu write_bytes %llu "
129  "cancelled_write_bytes %llu\n",
130  rchar, wchar, syscr, syscw,
131  read_bytes, write_bytes,
132  cancelled_write_bytes);
133  }
134  }
135 }
136 
137 #undef M0_TRACE_SUBSYSTEM
138 
139 /*
140  * Local variables:
141  * c-indentation-style: "K&R"
142  * c-basic-offset: 8
143  * tab-width: 8
144  * fill-column: 80
145  * scroll-step: 1
146  * End:
147  */
static size_t nr
Definition: dump.c:1505
#define NULL
Definition: misc.h:38
uint32_t m0_strtou32(const char *str, char **endptr, int base)
Definition: umisc.c:42
uint64_t m0_strtou64(const char *str, char **endptr, int base)
Definition: umisc.c:36
Definition: sock.c:887
return M0_RC(rc)
int i
Definition: dir.c:1033
return M0_ERR(-EOPNOTSUPP)
const char * name
Definition: trace.c:110
M0_INTERNAL void m0_performance_counters(char *buf, size_t buf_len)
Definition: umisc.c:82
M0_INTERNAL int m0_host_resolve(const char *name, char *buf, size_t bufsiz)
Definition: umisc.c:49
static uint64_t base
Definition: dump.c:1504
static void usage(void)
Definition: console.c:155
int32_t rc
Definition: trigger_fop.h:47