Commit 1889465a1c0c8403447aed4da0823ef2bb249473

Authored by Andi Kleen
Committed by Anthony Liguori
1 parent e3fc14c3

Allow setting qemu process name v2

Set the Linux process name to the name argument specified with name. I find
this useful to see which guests are taking CPU time in top.

This doesn't affect ps, which checks argv[0], but rewriting the
environment uses much more code, so I only used this simple way.

v2: Use separate process= argument, no prefixes.

Signed-off-by: Andi Kleen <ak@linux.intel.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing 2 changed files with 31 additions and 2 deletions
qemu-options.hx
... ... @@ -363,12 +363,14 @@ Network adapter that supports CDC ethernet and RNDIS protocols.
363 363 ETEXI
364 364  
365 365 DEF("name", HAS_ARG, QEMU_OPTION_name,
366   - "-name string set the name of the guest\n")
  366 + "-name string1[,process=string2] set the name of the guest\n"
  367 + " string1 sets the window title and string2 the process name (on Linux)\n")
367 368 STEXI
368 369 @item -name @var{name}
369 370 Sets the @var{name} of the guest.
370 371 This name will be displayed in the SDL window caption.
371 372 The @var{name} will also be used for the VNC server.
  373 +Also optionally set the top visible process name in Linux.
372 374 ETEXI
373 375  
374 376 DEF("uuid", HAS_ARG, QEMU_OPTION_uuid,
... ...
... ... @@ -68,6 +68,7 @@
68 68 #include <pty.h>
69 69 #include <malloc.h>
70 70 #include <linux/rtc.h>
  71 +#include <sys/prctl.h>
71 72  
72 73 /* For the benefit of older linux systems which don't supply it,
73 74 we use a local copy of hpet.h. */
... ... @@ -300,6 +301,20 @@ void hw_error(const char *fmt, ...)
300 301 va_end(ap);
301 302 abort();
302 303 }
  304 +
  305 +static void set_proc_name(const char *s)
  306 +{
  307 +#ifdef __linux__
  308 + char name[16];
  309 + if (!s)
  310 + return;
  311 + name[sizeof(name) - 1] = 0;
  312 + strncpy(name, s, sizeof(name));
  313 + /* Could rewrite argv[0] too, but that's a bit more complicated.
  314 + This simple way is enough for `top'. */
  315 + prctl(PR_SET_NAME, name);
  316 +#endif
  317 +}
303 318  
304 319 /***************/
305 320 /* ballooning */
... ... @@ -5416,7 +5431,19 @@ int main(int argc, char **argv, char **envp)
5416 5431 break;
5417 5432 #endif
5418 5433 case QEMU_OPTION_name:
5419   - qemu_name = optarg;
  5434 + qemu_name = qemu_strdup(optarg);
  5435 + {
  5436 + char *p = strchr(qemu_name, ',');
  5437 + if (p != NULL) {
  5438 + *p++ = 0;
  5439 + if (strncmp(p, "process=", 8)) {
  5440 + fprintf(stderr, "Unknown subargument %s to -name", p);
  5441 + exit(1);
  5442 + }
  5443 + p += 8;
  5444 + set_proc_name(p);
  5445 + }
  5446 + }
5420 5447 break;
5421 5448 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5422 5449 case QEMU_OPTION_prom_env:
... ...