Open main menu

OpenVZ Virtuozzo Containers Wiki β

Changes

Ubstat system call

538 bytes added, 14:06, 20 January 2011
no edit summary
This article describes an interesting system call which was designed to pick beancounters statistics.
== Overview ==
The system call appeared in the very first version of the OpenVZ. Its API is rather sloppy, but this is something we have to live with due to backward compatibility reasons.
The main intention of this system call is to allow a user space process get the beancounters statistics ''periodically''. This statistics includes the fields observed in the <code>/proc/user_beancounters</code> file and one more field - the so called <code>minheld</code> value which is opposite to the <code>maxheld</code> one. As long as exporting the statistics the system call also notifies the task about the desired period has elapsed. The notification is performed by sending a signal to a process and this notification is one-shot.
== How it works ==
The typical usage of this call is in performing the following steps.
# Request the amount of resources
In a signal handler one should just perform a respective system call to get the stats and schedule the next notification (yes, they are performed in one go; see below for more details).
== API ==
The system call description is
<presource lang="c">
long ubstat(int func, unsigned long luid, void *notif, void *buf, int size);
</presource>
The macros and data typed used are declared in <code>include/ub/ub_stat.h</code> file.
=== Arguments description ===
'''<code>func</code>''' is like <code>cmd</code> in the <code>ioctl</code> system call. It can be one of
'''<code>size</code>''' is the <code>buf</code> memory size.
=== The statistics format ===The format of data rerurned returned into the buffer depends on the function requested.
'''1. <code>UBSTAT_READ_ONE</code>''' format is
<presource lang="c">
typedef unsigned long ubstattime_t;
ubstatparm_t param[1];
}
</presource>
It contains the time period for which the stats are returned and the <code>maxheld</code> and <code>failcnt</code> for the resource.
'''2. <code>UBSTAT_READ_ALL</code>''' format is
<presource lang="c">
typedef unsigned long ubstattime_t;
ubstatparm_t param[UB_RESOURCES];
}
</presource>
It contains the same info as the <code>UBSTAT_READ_ONE</code> does, but for all the resources.
'''3. <code>UBSTAT_READ_FULL</code>''' format is
<presource lang="c">
typedef unsigned long ubstattime_t;
ubstatparmf_t param[UB_RESOURCES];
}
</presource>
It contains the extended info for all the resources.
'''6. UBSTAT_GETTIME''' format is
<presource lang="c">
typedef unsigned long ubstattime_t;
ubstattime_t cur_time;
};
</presource>
It returns the time interval within which the stats are collected and the current time.
All the times used are in seconds.
=== Notification ===
The notification info is passed via the <code>notif</code> argument and is being set up for all the functions except the <code>UBLIST</code> and the <code>UBPARNUM</code>. The notification is one-shot, but note that once you requested the statistics the next shot is scheduled at the same time.
The <code>notif</code> should point to
<presource lang="c">
typedef struct {
long maxinterval;
int signum;
} ubnotifrq_t;
</presource>
The <code>maxinterval</code> is the time after which the notification will be delivered. It should be more than 1 (second).
The <code>signum</code> is the signal that will be sent to notify.
=== Return value ===The system call returns -1 in case error has occurred. In case of <code>UBSTAT_UBPARMNUM</code> it returns <code>UB_RESOURCES</code> and in all other cases it returns the amount of bytes written to the <code>buf</code>. == Demo ==The following program demonstrates how you can (but not should) use the described API. This example is deliberately made very stupid and simple to demonstrate the main ideaand will only work on x86_64.<presource lang="c">
#include <stdio.h>
#include <unistd.h>
return 0;
}
</presource== Implementation constraints ==Unfortunately the API is not architecture independent and thus 32-bit application will simply not work on x86_64.
[[Category:UBC]]