| 1 | /* |
| 2 | * Copyright (c) 2017-2018 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
| 5 | * |
| 6 | * This file contains Original Code and/or Modifications of Original Code |
| 7 | * as defined in and that are subject to the Apple Public Source License |
| 8 | * Version 2.0 (the 'License'). You may not use this file except in |
| 9 | * compliance with the License. The rights granted to you under the License |
| 10 | * may not be used to create, or enable the creation or redistribution of, |
| 11 | * unlawful or unlicensed copies of an Apple operating system, or to |
| 12 | * circumvent, violate, or enable the circumvention or violation of, any |
| 13 | * terms of an Apple operating system software license agreement. |
| 14 | * |
| 15 | * Please obtain a copy of the License at |
| 16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
| 17 | * |
| 18 | * The Original Code and all software distributed under the License are |
| 19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
| 20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
| 23 | * Please see the License for the specific language governing rights and |
| 24 | * limitations under the License. |
| 25 | * |
| 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 27 | */ |
| 28 | |
| 29 | #include <netinet/in_stat.h> |
| 30 | |
| 31 | #define IN_STAT_ACTIVITY_GRANULARITY 8 /* 8 sec granularity */ |
| 32 | #define IN_STAT_ACTIVITY_TIME_SEC_SHIFT 3 /* 8 sec per bit */ |
| 33 | #define IN_STAT_ACTIVITY_BITMAP_TOTAL_SIZE ((uint64_t) 128) |
| 34 | #define IN_STAT_ACTIVITY_BITMAP_FIELD_SIZE ((uint64_t) 64) |
| 35 | #define IN_STAT_ACTIVITY_TOTAL_TIME ((uint64_t) (8 * 128)) |
| 36 | #define IN_STAT_SET_MOST_SIGNIFICANT_BIT ((u_int64_t )0x8000000000000000) |
| 37 | |
| 38 | void |
| 39 | in_stat_set_activity_bitmap(activity_bitmap_t *activity, uint64_t now) |
| 40 | { |
| 41 | uint64_t elapsed_time, slot; |
| 42 | uint64_t *bitmap; |
| 43 | if (activity->start == 0) { |
| 44 | // Align all activity maps |
| 45 | activity->start = now - (now % IN_STAT_ACTIVITY_GRANULARITY); |
| 46 | } |
| 47 | elapsed_time = now - activity->start; |
| 48 | |
| 49 | slot = elapsed_time >> IN_STAT_ACTIVITY_TIME_SEC_SHIFT; |
| 50 | if (slot < IN_STAT_ACTIVITY_BITMAP_TOTAL_SIZE) { |
| 51 | if (slot < IN_STAT_ACTIVITY_BITMAP_FIELD_SIZE) { |
| 52 | bitmap = &activity->bitmap[0]; |
| 53 | } else { |
| 54 | bitmap = &activity->bitmap[1]; |
| 55 | slot -= IN_STAT_ACTIVITY_BITMAP_FIELD_SIZE; |
| 56 | } |
| 57 | *bitmap |= (((u_int64_t) 1) << slot); |
| 58 | } else { |
| 59 | if (slot >= (IN_STAT_ACTIVITY_BITMAP_TOTAL_SIZE * 2)) { |
| 60 | activity->start = now - IN_STAT_ACTIVITY_TOTAL_TIME; |
| 61 | activity->bitmap[0] = activity->bitmap[1] = 0; |
| 62 | } else { |
| 63 | uint64_t shift = |
| 64 | slot - (IN_STAT_ACTIVITY_BITMAP_TOTAL_SIZE - 1); |
| 65 | /* |
| 66 | * Move the start time and bitmap forward to |
| 67 | * cover the lost time |
| 68 | */ |
| 69 | activity->start += |
| 70 | (shift << IN_STAT_ACTIVITY_TIME_SEC_SHIFT); |
| 71 | if (shift > IN_STAT_ACTIVITY_BITMAP_FIELD_SIZE) { |
| 72 | activity->bitmap[0] = activity->bitmap[1]; |
| 73 | activity->bitmap[1] = 0; |
| 74 | shift -= IN_STAT_ACTIVITY_BITMAP_FIELD_SIZE; |
| 75 | if (shift == IN_STAT_ACTIVITY_BITMAP_FIELD_SIZE) { |
| 76 | activity->bitmap[0] = 0; |
| 77 | } else { |
| 78 | activity->bitmap[0] >>= shift; |
| 79 | } |
| 80 | } else { |
| 81 | uint64_t mask_lower, tmp; |
| 82 | uint64_t b1_low, b0_high; |
| 83 | |
| 84 | /* |
| 85 | * generate a mask with all of lower |
| 86 | * 'shift' bits set |
| 87 | */ |
| 88 | tmp = (((uint64_t)1) << (shift - 1)); |
| 89 | mask_lower = ((tmp - 1) ^ tmp); |
| 90 | activity->bitmap[0] >>= shift; |
| 91 | b1_low = (activity->bitmap[1] & mask_lower); |
| 92 | |
| 93 | b0_high = (b1_low << |
| 94 | (IN_STAT_ACTIVITY_BITMAP_FIELD_SIZE - |
| 95 | shift)); |
| 96 | activity->bitmap[0] |= b0_high; |
| 97 | activity->bitmap[1] >>= shift; |
| 98 | } |
| 99 | } |
| 100 | activity->bitmap[1] |= IN_STAT_SET_MOST_SIGNIFICANT_BIT; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | in_stat_clear_activity_bitmap(activity_bitmap_t *activity) |
| 106 | { |
| 107 | activity->start = 0; |
| 108 | activity->bitmap[0] = 0; |
| 109 | activity->bitmap[1] = 0; |
| 110 | } |
| 111 | |