]>
git.proxmox.com Git - ovs.git/blob - lib/memory.c
2 * Copyright (c) 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <sys/resource.h>
22 #include "dynamic-string.h"
23 #include "poll-loop.h"
29 VLOG_DEFINE_THIS_MODULE(memory
);
31 /* The number of milliseconds before the first report of daemon memory usage,
32 * and the number of milliseconds between checks for daemon memory growth. */
33 #define MEMORY_CHECK_INTERVAL (10 * 1000)
35 /* When we should next check memory usage and possibly trigger a report. */
36 static long long int next_check
;
38 /* The last time at which we reported memory usage, and the usage we reported
40 static long long int last_report
;
41 static unsigned long int last_reported_maxrss
;
43 /* Are we expecting a call to memory_report()? */
44 static bool want_report
;
46 /* Unixctl connections waiting for responses. */
47 static struct unixctl_conn
**conns
;
48 static size_t n_conns
;
50 static void memory_init(void);
52 /* Runs the memory monitor.
54 * The client should call memory_should_report() afterward.
56 * This function, and the remainder of this module's interface, should be
57 * called from only a single thread. */
66 /* Time for a check? */
68 if (now
< next_check
) {
71 next_check
= now
+ MEMORY_CHECK_INTERVAL
;
73 /* Time for a report? */
74 getrusage(RUSAGE_SELF
, &usage
);
75 if (!last_reported_maxrss
) {
76 VLOG_INFO("%lu kB peak resident set size after %.1f seconds",
77 (unsigned long int) usage
.ru_maxrss
,
78 (now
- time_boot_msec()) / 1000.0);
79 } else if (usage
.ru_maxrss
>= last_reported_maxrss
* 1.5) {
80 VLOG_INFO("peak resident set size grew %.0f%% in last %.1f seconds, "
81 "from %lu kB to %lu kB",
82 ((double) usage
.ru_maxrss
/ last_reported_maxrss
- 1) * 100,
83 (now
- last_report
) / 1000.0,
84 last_reported_maxrss
, (unsigned long int) usage
.ru_maxrss
);
89 /* Request a report. */
92 last_reported_maxrss
= usage
.ru_maxrss
;
95 /* Causes the poll loop to wake up if the memory monitor needs to run. */
99 if (memory_should_report()) {
100 poll_immediate_wake();
104 /* Returns true if the caller should log some information about memory usage
105 * (with memory_report()), false otherwise. */
107 memory_should_report(void)
109 return want_report
|| n_conns
> 0;
113 compose_report(const struct simap
*usage
, struct ds
*s
)
115 const struct simap_node
**nodes
= simap_sort(usage
);
116 size_t n
= simap_count(usage
);
119 for (i
= 0; i
< n
; i
++) {
120 const struct simap_node
*node
= nodes
[i
];
122 ds_put_format(s
, "%s:%u ", node
->name
, node
->data
);
128 /* Logs the contents of 'usage', as a collection of name-count pairs.
130 * 'usage' should capture large-scale statistics that one might reasonably
131 * expect to correlate with memory usage. For example, each OpenFlow flow
132 * requires some memory, so ovs-vswitchd includes the total number of flows in
135 memory_report(const struct simap
*usage
)
141 compose_report(usage
, &s
);
145 VLOG_INFO("%s", ds_cstr(&s
));
150 for (i
= 0; i
< n_conns
; i
++) {
151 unixctl_command_reply(conns
[i
], ds_cstr(&s
));
162 memory_unixctl_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
163 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
165 conns
= xrealloc(conns
, (n_conns
+ 1) * sizeof *conns
);
166 conns
[n_conns
++] = conn
;
172 static bool inited
= false;
176 unixctl_command_register("memory/show", "", 0, 0,
177 memory_unixctl_show
, NULL
);
179 next_check
= time_boot_msec() + MEMORY_CHECK_INTERVAL
;