【问题标题】:Maximum value of kernel statistics - python内核统计的最大值 - python
【发布时间】:2015-03-16 20:48:29
【问题描述】:

问题:内核统计计数器的最大值是多少,在python代码中如何处理?

上下文:我根据内核统计数据计算了一些统计数据(例如 /proc/partitions - 它将是定制的 python iostat 版本)。但我有溢出值的问题 - 负值。 iostat原码https://github.com/sysstat/sysstat/blob/master/iostat.ccmets:

* Counters overflows are possible, but don't need to be handled in
* a special way: The difference is still properly calculated if the
* result is of the same type as the two values.

我的语言是 python,在我的情况下我需要关心溢出。可能它也取决于架构(32/64)。我试过2^64-1(64位系统),但没有成功。

【问题讨论】:

    标签: python c linux integer-overflow iostat


    【解决方案1】:

    以下函数适用于 32 位计数器:

    def calc_32bit_diff(old, new):
      return (new - old + 0x100000000) % 0x100000000
    
    print calc_32bit_diff(1, 42)
    print calc_32bit_diff(2147483647, -2147483648)
    print calc_32bit_diff(-2147483648, 2147483647)
    

    这显然是行不通的,因为计数器在两次连续读取之间循环不止一次(但随后也没有其他方法可以工作,因为信息已经不可恢复地丢失了)。

    编写 64 位版本的内容留给读者作为练习。 :)

    【讨论】:

      猜你喜欢
      • 2021-01-11
      • 1970-01-01
      • 2016-06-03
      • 2014-04-24
      • 1970-01-01
      • 2015-07-10
      • 1970-01-01
      • 2014-11-27
      • 1970-01-01
      相关资源
      最近更新 更多