浏览代码

trivial: doc: hpfall: reduce risk that hpfall can do harm

Improve the example code to be at least useable, as in not causing
harm (as shown below). Code can still be improved further, but this
adds some basic safeguards.

1. hpfall *MUST* mlockall(MCL_CURRENT|MCL_FUTURE); itself!
Since the Program sits and waits most of the time it becomes very likely
swapped out. If it gets woken up when the laptop drops from the table
while it is swapped out it actually triggers harddrive activity!

2. Daemonize hpfall using 'daemon(0,0)' (quick and dirty).

3. Give hpfall realtime priority.
Should give a chance that it has less latency when woken up.

Signed-off-by: Christian Thaeter <ct@pipapo.org>
Signed-off-by: Frans Pop <elendil@planet.nl>
Acked-by: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
Christian Thaeter 16 年之前
父节点
当前提交
2bace8b951
共有 1 个文件被更改,包括 8 次插入0 次删除
  1. 8 0
      Documentation/hwmon/hpfall.c

+ 8 - 0
Documentation/hwmon/hpfall.c

@@ -16,6 +16,8 @@
 #include <stdint.h>
 #include <stdint.h>
 #include <errno.h>
 #include <errno.h>
 #include <signal.h>
 #include <signal.h>
+#include <sys/mman.h>
+#include <sched.h>
 
 
 void write_int(char *path, int i)
 void write_int(char *path, int i)
 {
 {
@@ -62,6 +64,7 @@ void ignore_me(void)
 int main(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
 {
 	int fd, ret;
 	int fd, ret;
+	struct sched_param param;
 
 
 	fd = open("/dev/freefall", O_RDONLY);
 	fd = open("/dev/freefall", O_RDONLY);
 	if (fd < 0) {
 	if (fd < 0) {
@@ -69,6 +72,11 @@ int main(int argc, char *argv[])
 		return EXIT_FAILURE;
 		return EXIT_FAILURE;
 	}
 	}
 
 
+	daemon(0, 0);
+	param.sched_priority = sched_get_priority_max(SCHED_FIFO);
+	sched_setscheduler(0, SCHED_FIFO, &param);
+	mlockall(MCL_CURRENT|MCL_FUTURE);
+
 	signal(SIGALRM, ignore_me);
 	signal(SIGALRM, ignore_me);
 
 
 	for (;;) {
 	for (;;) {