Class: Pacct::Entry

Inherits:
Object
  • Object
show all
Defined in:
ext/pacct/pacct_c.c,
ext/pacct/pacct_c.c

Overview

Represents an entry in a Pacct::File

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.newObject

This is the version of pacct_entry_new that is actually exposed to Ruby.



242
243
244
# File 'ext/pacct/pacct_c.c', line 242

static VALUE ruby_pacct_entry_new(VALUE self) {
  return pacct_entry_new(NULL);
}

Instance Method Details

#command_nameObject

Returns the first 15 characters of the command name



666
667
668
669
670
671
# File 'ext/pacct/pacct_c.c', line 666

static VALUE get_command_name(VALUE self) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  return rb_str_new2(data->ac_comm);
}

#command_name=(name) ⇒ Object

Sets the first 15 characters of the command name



676
677
678
679
680
681
682
683
684
# File 'ext/pacct/pacct_c.c', line 676

static VALUE set_command_name(VALUE self, VALUE name) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  strncpy(data->ac_comm, StringValueCStr(name), ACCT_COMM - 1);
  data->ac_comm[ACCT_COMM - 1] = '\0';
  
  return Qnil;
}

#cpu_timeObject

Returns the task’s total CPU time in seconds



589
590
591
592
593
594
# File 'ext/pacct/pacct_c.c', line 589

static VALUE get_cpu_time(VALUE self) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  return INT2NUM((comp_t_to_ulong(data->ac_utime) + comp_t_to_ulong(data->ac_stime)) / ticksPerSecond);
}

#exit_codeObject

Returns the command’s exit code



689
690
691
692
693
694
# File 'ext/pacct/pacct_c.c', line 689

static VALUE get_exit_code(VALUE self) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  return INT2NUM(data->ac_exitcode);
}

#exit_code=(value) ⇒ Object

Sets the command’s exit code



699
700
701
702
703
704
705
706
# File 'ext/pacct/pacct_c.c', line 699

static VALUE set_exit_code(VALUE self, VALUE value) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  data->ac_exitcode = NUM2UINT(value);
  
  return Qnil;
}

#group_idObject

Returns the group ID of the user who executed the command



462
463
464
465
466
467
# File 'ext/pacct/pacct_c.c', line 462

static VALUE get_group_id(VALUE self) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  return INT2NUM(data->ac_gid);
}

#group_nameObject

Returns the group name of the user who executed the command



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'ext/pacct/pacct_c.c', line 472

static VALUE get_group_name(VALUE self) {
  struct acct_v3* data;
  struct group* group_data;
  VALUE name, id;
  Data_Get_Struct(self, struct acct_v3, data);
  
  id = UINT2NUM(data->ac_gid);
  name = rb_hash_aref(known_groups_by_id, id);
  if(name != Qnil) {
    return name;
  }
  
  errno = 0;
  group_data = getgrgid(data->ac_gid);
  if(!group_data) {
    char buf[512];
    VALUE err;
    int e = errno;
    snprintf(buf, 512, "Unable to obtain group name for ID %u", data->ac_gid);
    if(e == 0) {
      e = ENODATA;
    }
    err = rb_funcall(cSystemCallError, id_new, 2, rb_str_new2(buf), INT2NUM(e));
    rb_exc_raise(err);
  }
  
  name = rb_str_new2(group_data->gr_name);
  rb_hash_aset(known_groups_by_id, id, name);
  
  return name;
}

#group_name=(name) ⇒ Object

Sets the group name of the user who executed the command



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'ext/pacct/pacct_c.c', line 507

static VALUE set_group_name(VALUE self, VALUE name) {
  struct acct_v3* data;
  struct group* group_data;
  VALUE id;
  char* c_name = StringValueCStr(name);
  Data_Get_Struct(self, struct acct_v3, data);
  
  id = rb_hash_aref(known_groups_by_name, name);
  if(id != Qnil) {
    data->ac_gid = NUM2UINT(id);
    return Qnil;
  }
  
  errno = 0;
  group_data = getgrnam(c_name);
  if(!group_data) {
    char buf[512];
    VALUE err;
    int e = errno;
    snprintf(buf, 512, "Unable to obtain group ID for name '%s'", c_name);
    if(e == 0) {
      e = ENODATA;
    } 
    err = rb_funcall(cSystemCallError, id_new, 2, rb_str_new2(buf), INT2NUM(e));
    rb_exc_raise(err);
  }
  
  id = UINT2NUM(group_data->gr_gid);
  rb_hash_aset(known_groups_by_name, name, id);
  
  data->ac_gid = group_data->gr_gid;
  
  return Qnil;
}

#memoryObject

Returns the task’s average memory usage in kilobytes



643
644
645
646
647
648
649
# File 'ext/pacct/pacct_c.c', line 643

static VALUE get_average_mem_usage(VALUE self) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  //Why divided by page size?
  return INT2NUM(comp_t_to_ulong(data->ac_mem) * 1024 / pageSize);
}

#memory=(value) ⇒ Object

Sets the task’s average memory usage in kilobytes



654
655
656
657
658
659
660
661
# File 'ext/pacct/pacct_c.c', line 654

static VALUE set_average_mem_usage(VALUE self, VALUE value) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  data->ac_mem = ulong_to_comp_t(NUM2ULONG(value) * pageSize / 1024);
  
  return Qnil;
}

#process_idObject

Returns the process ID



354
355
356
357
358
359
# File 'ext/pacct/pacct_c.c', line 354

static VALUE get_process_id(VALUE self) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  return INT2NUM(data->ac_pid);
}

#process_id=(pid) ⇒ Object

Sets the process ID



364
365
366
367
368
369
370
371
# File 'ext/pacct/pacct_c.c', line 364

static VALUE set_process_id(VALUE self, VALUE pid) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  data->ac_pid = NUM2UINT(pid);
  
  return Qnil;
}

#start_timeObject

Returns the task’s start time



621
622
623
624
625
626
# File 'ext/pacct/pacct_c.c', line 621

static VALUE get_start_time(VALUE self) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  return rb_funcall(cTime, id_at, 1, INT2NUM(data->ac_btime));
}

#start_time=(value) ⇒ Object

Sets the task’s start time



631
632
633
634
635
636
637
638
# File 'ext/pacct/pacct_c.c', line 631

static VALUE set_start_time(VALUE self, VALUE value) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  data->ac_btime = NUM2UINT(rb_funcall(value, id_to_i, 0));
  
  return Qnil;
}

#system_timeObject

Returns the task’s total system CPU time in seconds



567
568
569
570
571
572
# File 'ext/pacct/pacct_c.c', line 567

static VALUE get_system_time(VALUE self) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  return INT2NUM(comp_t_to_ulong(data->ac_stime) / ticksPerSecond);
}

#system_time=(value) ⇒ Object

Sets the task’s total system CPU time



577
578
579
580
581
582
583
584
# File 'ext/pacct/pacct_c.c', line 577

static VALUE set_system_time(VALUE self, VALUE value) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  data->ac_stime = ulong_to_comp_t(NUM2ULONG(value) * ticksPerSecond);
  
  return Qnil;
}

#user_idObject

Returns the ID of the user who executed the command



376
377
378
379
380
381
# File 'ext/pacct/pacct_c.c', line 376

static VALUE get_user_id(VALUE self) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  return INT2NUM(data->ac_uid);
}

#user_nameObject

Returns the name of the user who executed the command



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'ext/pacct/pacct_c.c', line 386

static VALUE get_user_name(VALUE self) {
  struct acct_v3* data;
  struct passwd* pw_data;
  VALUE id, name;
  Data_Get_Struct(self, struct acct_v3, data);
  
  //If there's a cached user name, return it.
  id = UINT2NUM(data->ac_uid);
  name = rb_hash_aref(known_users_by_id, id);
  if(name != Qnil) {
    return name;
  }
  
  //Otherwise, get the user name from the OS.
  errno = 0;
  pw_data = getpwuid(data->ac_uid);
  if(!pw_data) {
    char buf[512];
    VALUE err;
    int e = errno;
    snprintf(buf, 512, "Unable to obtain user name for ID %u", data->ac_uid);
    if(e == 0) {
        e = ENODATA;
    }
    err = rb_funcall(cSystemCallError, id_new, 2, rb_str_new2(buf), INT2NUM(e));
    rb_exc_raise(err);
  }
  
  //Cache the user name.
  name = rb_str_new2(pw_data->pw_name);
  rb_hash_aset(known_users_by_id, id, name);
  
  return name;
}

#user_name=(name) ⇒ Object

Sets the name of the user who executed the command



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'ext/pacct/pacct_c.c', line 424

static VALUE set_user_name(VALUE self, VALUE name) {
  struct acct_v3* data;
  struct passwd* pw_data;
  char* c_name = StringValueCStr(name);
  VALUE id;
  Data_Get_Struct(self, struct acct_v3, data);
  
  id = rb_hash_aref(known_users_by_name, name);
  if(id != Qnil) {
    data->ac_uid = NUM2UINT(id);
    return Qnil;
  }
  
  errno = 0;
  pw_data = getpwnam(c_name);
  if(!pw_data) {
    char buf[512];
    VALUE err;
    int e = errno;
    snprintf(buf, 512, "Unable to obtain user ID for name '%s'", c_name);
    if(e == 0) {
        e = ENODATA;
    }
    err = rb_funcall(cSystemCallError, id_new, 2, rb_str_new2(buf), INT2NUM(e));
    rb_exc_raise(err);
  }
  
  id = UINT2NUM(pw_data->pw_uid);
  rb_hash_aset(known_users_by_name, name, id);
  
  data->ac_uid = pw_data->pw_uid;
  
  return Qnil;
}

#user_timeObject

Returns the task’s total user CPU time in seconds



545
546
547
548
549
550
# File 'ext/pacct/pacct_c.c', line 545

static VALUE get_user_time(VALUE self) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  return INT2NUM(comp_t_to_ulong(data->ac_utime) / ticksPerSecond);
}

#user_time=(value) ⇒ Object

Sets the task’s total user CPU time



555
556
557
558
559
560
561
562
# File 'ext/pacct/pacct_c.c', line 555

static VALUE set_user_time(VALUE self, VALUE value) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  data->ac_utime = ulong_to_comp_t(NUM2ULONG(value) * ticksPerSecond);
  
  return Qnil;
}

#wall_timeObject

Returns the task’s total wall time in seconds



599
600
601
602
603
604
# File 'ext/pacct/pacct_c.c', line 599

static VALUE get_wall_time(VALUE self) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  return rb_float_new(data->ac_etime);
}

#wall_time=(value) ⇒ Object

Sets the task’s total wall time



609
610
611
612
613
614
615
616
# File 'ext/pacct/pacct_c.c', line 609

static VALUE set_wall_time(VALUE self, VALUE value) {
  struct acct_v3* data;
  Data_Get_Struct(self, struct acct_v3, data);
  
  data->ac_etime = NUM2DBL(value);
  
  return Qnil;
}