SORU
14 EYLÜL 2009, PAZARTESİ


Hesaplama, Linux işlemin CPU kullanımı

Programlama yoluyla [C] Linux verilen işlem KİMLİĞİ için CPU kullanımı % hesaplamak istiyorum.

Nasıl belirli bir süreç için gerçek zamanlı CPU kullanımı % alabilir miyiz ??

Net daha fazla yapmak için

  • Sağlanan processıd veya işleminin CPU kullanımı belirlemek gerekir.
  • Süreç, alt süreç olması gerekmez.
  • Çözüm istiyorum 'C' dili.

CEVAP
15 EYLÜL 2009, Salı


/proc/<PID>/stat verileri ayrıştırmak gerekir. Bu ilk birkaç alanları (çekirdek kaynak Documentation/filesystems/proc.txt)

Table 1-3: Contents of the stat files (as of 2.6.22-rc3)
..............................................................................
 Field          Content
  pid           process id
  tcomm         filename of the executable
  state         state (R is running, S is sleeping, D is sleeping in an
                uninterruptible wait, Z is zombie, T is traced or stopped)
  ppid          process id of the parent process
  pgrp          pgrp of the process
  sid           session id
  tty_nr        tty the process uses
  tty_pgrp      pgrp of the tty
  flags         task flags
  min_flt       number of minor faults
  cmin_flt      number of minor faults with child's
  maj_flt       number of major faults
  cmaj_flt      number of major faults with child's
  utime         user mode jiffies
  stime         kernel mode jiffies
  cutime        user mode jiffies with child's
  cstime        kernel mode jiffies with child's

Muhtemelen utime ve/veya stime peşindesin. Ayrıca gibi görünüyor /proc/stat, cpu satırı okumak gerekir:

cpu  192369 7119 480152 122044337 14142 9937 26747 0 0

Bu size çeşitli kategorilerde kullanılan toplam CPU zamanı, saniye cinsinden söyler. Bu satırdaki değerlerin toplamını almak time_total bir tedbir almak gerekir.

İlgilendiğiniz işlemi için utime stime okuma /proc/stat time_total okuyun. Sonra bir süre uyku, ve hepsini tekrar okuyun. Şimdi örnekleme zamanla işleminin CPU kullanımı hesaplayabilirsiniz.

user_util = 100 * (utime_after - utime_before) / (time_total_after - time_total_before);
sys_util = 100 * (stime_after - stime_before) / (time_total_after - time_total_before);

Mantıklı mı?

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Jonah Penna

    Jonah Penna

    11 EYLÜL 2005
  • krotoflik

    krotoflik

    26 ŞUBAT 2011
  • MugenPowerBatteries

    MugenPowerBa

    8 EKİM 2010