linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: "Kirill A. Shutemov" <kirill@shutemov.name>
To: balbir@linux.vnet.ibm.com
Cc: containers@lists.linux-foundation.org, linux-mm@kvack.org,
	Paul Menage <menage@google.com>, Li Zefan <lizf@cn.fujitsu.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>,
	Pavel Emelyanov <xemul@openvz.org>,
	Dan Malek <dan@embeddedalley.com>,
	Vladislav Buzov <vbuzov@embeddedalley.com>,
	Daisuke Nishimura <nishimura@mxp.nes.nec.co.jp>,
	Alexander Shishkin <virtuoso@slind.org>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 0/4] cgroup notifications API and memory thresholds
Date: Sun, 27 Dec 2009 20:37:57 +0200	[thread overview]
Message-ID: <cc557aab0912271037scb29fe1xcebe9adfaea97b24@mail.gmail.com> (raw)
In-Reply-To: <20091227124732.GA3601@balbir.in.ibm.com>

[-- Attachment #1: Type: text/plain, Size: 2033 bytes --]

On Sun, Dec 27, 2009 at 2:47 PM, Balbir Singh <balbir@linux.vnet.ibm.com> wrote:
> * Kirill A. Shutemov <kirill@shutemov.name> [2009-12-27 04:08:58]:
>
>> This patchset introduces eventfd-based API for notifications in cgroups and
>> implements memory notifications on top of it.
>>
>> It uses statistics in memory controler to track memory usage.
>>
>> Output of time(1) on building kernel on tmpfs:
>>
>> Root cgroup before changes:
>>       make -j2  506.37 user 60.93s system 193% cpu 4:52.77 total
>> Non-root cgroup before changes:
>>       make -j2  507.14 user 62.66s system 193% cpu 4:54.74 total
>> Root cgroup after changes (0 thresholds):
>>       make -j2  507.13 user 62.20s system 193% cpu 4:53.55 total
>> Non-root cgroup after changes (0 thresholds):
>>       make -j2  507.70 user 64.20s system 193% cpu 4:55.70 total
>> Root cgroup after changes (1 thresholds, never crossed):
>>       make -j2  506.97 user 62.20s system 193% cpu 4:53.90 total
>> Non-root cgroup after changes (1 thresholds, never crossed):
>>       make -j2  507.55 user 64.08s system 193% cpu 4:55.63 total
>>
>> Any comments?
>
> Thanks for adding the documentation, now on to more critical questions
>
> 1. Any reasons for not using cgroupstats?

Could you explain the idea? I don't see how cgroupstats applicable for
the task.

> 2. Is there a user space test application to test this code.

Attached. It's not very clean, but good enough for testing propose.
Example of usage:

$ echo '/cgroups/memory.usage_in_bytes 1G' | ./cgroup_event_monitor

>  IIUC,
> I need to write a program that uses eventfd(2) and then passes
> the eventfd descriptor and thresold to cgroup.*event* file and
> then the program will get notified when the threshold is reached?

You need to pass eventfd descriptor, descriptor of control file to be
monitored (memory.usage_in_bytes or memory.memsw.usage_in_bytes) and
threshold.

Do you want to rename cgroup.event_control to cgroup.event?

[-- Attachment #2: cgroup_event_monitor.c --]
[-- Type: application/octet-stream, Size: 2185 bytes --]

#define _GNU_SOURCE

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/eventfd.h>

#define MAX_EFD 100

struct event {
	char *path;
	char *args;
};

int main(int argc, char **argv)
{
	struct event events[MAX_EFD];
	struct pollfd pollfds[MAX_EFD];
	int n = 0;
	char line[PATH_MAX];
	int ret;

	while (fgets(line, PATH_MAX, stdin)) {
		int event_control, cfd;
		char *args = strchrnul(line, ' ');

		if (n >= MAX_EFD) {
			fprintf(stderr, "Too many events registered\n");
		}

		if (*args)
			*args++ = '\0';

		events[n].path = malloc(strlen(line) + 1);
		assert(events[n].path);
		strcpy(events[n].path, line);

		args[strlen(args) - 1] = '\0';
		events[n].args = malloc(strlen(args) + 1);
		assert(events[n].args);
		strcpy(events[n].args, args);

		cfd = open(events[n].path, O_RDONLY);
		if (cfd < 0) {
			fprintf(stderr, "Cannot open %s: %s\n", events[n].path,
					strerror(errno));
			return 1;
		}

		pollfds[n].events = POLLIN;
		pollfds[n].fd = eventfd(0, 0);

		dirname(line);
		strcat(line, "/cgroup.event_control");

		event_control = open(line, O_WRONLY);
		if (event_control < 0) {
			fprintf(stderr, "Cannot open %s: %s\n", line,
					strerror(errno));
			return 1;
		}

		snprintf(line, PATH_MAX, "%d %d %s\n", pollfds[n].fd, cfd,
				events[n].args);

		if (write(event_control, line, strlen(line)) < 0) {
			fprintf(stderr, "Cannot write to cgroup.event_control: %s\n",
					strerror(errno));
			return 1;
		}

		close(event_control);
		close(cfd);

		n++;
	}

	while((ret = poll(pollfds, n, -1)) != 0) {
		int i;

		if (ret < 0) {
			perror("poll()");
			return 1;
		}

		for(i=0; (i < n) && ret; i++) {
			if (pollfds[i].revents & POLLIN) {
				printf("%s %s\n", events[i].path,
						events[i].args);
				if (read(pollfds[i].fd, line, 8) < 0) {
					perror("read()");
					return 1;
				}
				ret--;
			}

			if (pollfds[i].revents & ~(POLLIN)) {
				printf("%s(%s): unexpected event: %08x\n",
						events[i].path,
						events[i].args,
						pollfds[i].revents);
				return 1;
			}
		}
	}

	return 0;
}

  reply	other threads:[~2009-12-27 18:37 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-27  2:08 Kirill A. Shutemov
2009-12-27  2:08 ` [PATCH v4 1/4] cgroup: implement eventfd-based generic API for notifications Kirill A. Shutemov
2009-12-27  2:09   ` [PATCH v4 2/4] memcg: extract mem_group_usage() from mem_cgroup_read() Kirill A. Shutemov
2009-12-27  2:09     ` [PATCH v4 3/4] memcg: rework usage of stats by soft limit Kirill A. Shutemov
2009-12-27  2:09       ` [PATCH v4 4/4] memcg: implement memory thresholds Kirill A. Shutemov
2009-12-28  2:43         ` KAMEZAWA Hiroyuki
2009-12-28  3:23           ` Kirill A. Shutemov
2009-12-28  4:14             ` KAMEZAWA Hiroyuki
2009-12-28  4:42         ` Daisuke Nishimura
2009-12-30 13:03           ` Kirill A. Shutemov
2009-12-28  2:28       ` [PATCH v4 3/4] memcg: rework usage of stats by soft limit KAMEZAWA Hiroyuki
2009-12-28  2:37       ` Daisuke Nishimura
2009-12-28  2:30     ` [PATCH v4 2/4] memcg: extract mem_group_usage() from mem_cgroup_read() KAMEZAWA Hiroyuki
2009-12-28  2:31   ` [PATCH v4 1/4] cgroup: implement eventfd-based generic API for notifications KAMEZAWA Hiroyuki
2009-12-27 12:47 ` [PATCH v4 0/4] cgroup notifications API and memory thresholds Balbir Singh
2009-12-27 18:37   ` Kirill A. Shutemov [this message]
2010-01-04  0:15     ` Balbir Singh
2009-12-28  2:27 ` KAMEZAWA Hiroyuki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cc557aab0912271037scb29fe1xcebe9adfaea97b24@mail.gmail.com \
    --to=kirill@shutemov.name \
    --cc=akpm@linux-foundation.org \
    --cc=balbir@linux.vnet.ibm.com \
    --cc=containers@lists.linux-foundation.org \
    --cc=dan@embeddedalley.com \
    --cc=kamezawa.hiroyu@jp.fujitsu.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lizf@cn.fujitsu.com \
    --cc=menage@google.com \
    --cc=nishimura@mxp.nes.nec.co.jp \
    --cc=vbuzov@embeddedalley.com \
    --cc=virtuoso@slind.org \
    --cc=xemul@openvz.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox