sysinfo.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <sys/sysinfo.h>
  2. #include "syscall.h"
  3. #define klong long long
  4. #define kulong unsigned long long
  5. struct kernel_sysinfo {
  6. klong uptime;
  7. kulong loads[3];
  8. kulong totalram;
  9. kulong freeram;
  10. kulong sharedram;
  11. kulong bufferram;
  12. kulong totalswap;
  13. kulong freeswap;
  14. short procs;
  15. short pad;
  16. kulong totalhigh;
  17. kulong freehigh;
  18. unsigned mem_unit;
  19. };
  20. int __x32_sysinfo(struct sysinfo *info)
  21. {
  22. struct kernel_sysinfo tmp;
  23. int ret = syscall(SYS_sysinfo, &tmp);
  24. if(ret == -1) return ret;
  25. info->uptime = tmp.uptime;
  26. info->loads[0] = tmp.loads[0];
  27. info->loads[1] = tmp.loads[1];
  28. info->loads[2] = tmp.loads[2];
  29. kulong shifts;
  30. kulong max = tmp.totalram | tmp.totalswap;
  31. __asm__("bsr %1,%0" : "=r"(shifts) : "r"(max));
  32. shifts = shifts >= 32 ? shifts - 31 : 0;
  33. info->totalram = tmp.totalram >> shifts;
  34. info->freeram = tmp.freeram >> shifts;
  35. info->sharedram = tmp.sharedram >> shifts;
  36. info->bufferram = tmp.bufferram >> shifts;
  37. info->totalswap = tmp.totalswap >> shifts;
  38. info->freeswap = tmp.freeswap >> shifts;
  39. info->procs = tmp.procs ;
  40. info->totalhigh = tmp.totalhigh >> shifts;
  41. info->freehigh = tmp.freehigh >> shifts;
  42. info->mem_unit = (tmp.mem_unit ? tmp.mem_unit : 1) << shifts;
  43. return ret;
  44. }