procstat.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // +build aix
  2. package perfstat
  3. /*
  4. #cgo LDFLAGS: -lperfstat
  5. #include <libperfstat.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include "c_helpers.h"
  9. */
  10. import "C"
  11. import (
  12. "fmt"
  13. "unsafe"
  14. )
  15. func ProcessStat() ([]Process, error) {
  16. var proc *C.perfstat_process_t
  17. var first C.perfstat_id_t
  18. numproc := C.perfstat_process(nil, nil, C.sizeof_perfstat_process_t, 0)
  19. if numproc < 1 {
  20. return nil, fmt.Errorf("perfstat_process() error")
  21. }
  22. plen := C.sizeof_perfstat_process_t * C.ulong(numproc)
  23. proc = (*C.perfstat_process_t)(C.malloc(plen))
  24. defer C.free(unsafe.Pointer(proc))
  25. C.strcpy(&first.name[0], C.CString(""))
  26. r := C.perfstat_process(&first, proc, C.sizeof_perfstat_process_t, numproc)
  27. if r < 0 {
  28. return nil, fmt.Errorf("perfstat_process() error")
  29. }
  30. ps := make([]Process, r)
  31. for i := 0; i < int(r); i++ {
  32. p := C.get_process_stat(proc, C.int(i))
  33. if p != nil {
  34. ps[i] = perfstatprocess2process(p)
  35. }
  36. }
  37. return ps, nil
  38. }
  39. func ThreadStat() ([]Thread, error) {
  40. var thread *C.perfstat_thread_t
  41. var first C.perfstat_id_t
  42. numthr := C.perfstat_thread(nil, nil, C.sizeof_perfstat_thread_t, 0)
  43. if numthr < 1 {
  44. return nil, fmt.Errorf("perfstat_thread() error")
  45. }
  46. thlen := C.sizeof_perfstat_thread_t * C.ulong(numthr)
  47. thread = (*C.perfstat_thread_t)(C.malloc(thlen))
  48. defer C.free(unsafe.Pointer(thread))
  49. C.strcpy(&first.name[0], C.CString(""))
  50. r := C.perfstat_thread(&first, thread, C.sizeof_perfstat_thread_t, numthr)
  51. if r < 0 {
  52. return nil, fmt.Errorf("perfstat_thread() error")
  53. }
  54. th := make([]Thread, r)
  55. for i := 0; i < int(r); i++ {
  56. t := C.get_thread_stat(thread, C.int(i))
  57. if t != nil {
  58. th[i] = perfstatthread2thread(t)
  59. }
  60. }
  61. return th, nil
  62. }