procstat.go 1.7 KB

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