ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "src/core/iomgr/alarm_heap.h" |
| 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include <grpc/support/alloc.h> |
| 39 | #include <grpc/support/useful.h> |
| 40 | |
| 41 | /* Adjusts a heap so as to move a hole at position i closer to the root, |
| 42 | until a suitable position is found for element t. Then, copies t into that |
| 43 | position. This functor is called each time immediately after modifying a |
| 44 | value in the underlying container, with the offset of the modified element as |
| 45 | its argument. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 46 | static void adjust_upwards(grpc_alarm **first, gpr_uint32 i, grpc_alarm *t) { |
| 47 | while (i > 0) { |
| 48 | gpr_uint32 parent = (gpr_uint32)(((int)i - 1) / 2); |
| 49 | if (gpr_time_cmp(first[parent]->deadline, t->deadline) >= 0) break; |
| 50 | first[i] = first[parent]; |
| 51 | first[i]->heap_index = i; |
| 52 | i = parent; |
| 53 | } |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 54 | first[i] = t; |
| 55 | t->heap_index = i; |
| 56 | } |
| 57 | |
| 58 | /* Adjusts a heap so as to move a hole at position i farther away from the root, |
| 59 | until a suitable position is found for element t. Then, copies t into that |
| 60 | position. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 61 | static void adjust_downwards(grpc_alarm **first, gpr_uint32 i, |
| 62 | gpr_uint32 length, grpc_alarm *t) { |
| 63 | for (;;) { |
| 64 | gpr_uint32 left_child = 1u + 2u * i; |
| 65 | gpr_uint32 right_child; |
| 66 | gpr_uint32 next_i; |
| 67 | if (left_child >= length) break; |
| 68 | right_child = left_child + 1; |
| 69 | next_i = right_child < length && |
| 70 | gpr_time_cmp(first[left_child]->deadline, |
| 71 | first[right_child]->deadline) < 0 |
| 72 | ? right_child |
| 73 | : left_child; |
| 74 | if (gpr_time_cmp(t->deadline, first[next_i]->deadline) >= 0) break; |
| 75 | first[i] = first[next_i]; |
| 76 | first[i]->heap_index = i; |
| 77 | i = next_i; |
| 78 | } |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 79 | first[i] = t; |
| 80 | t->heap_index = i; |
| 81 | } |
| 82 | |
| 83 | #define SHRINK_MIN_ELEMS 8 |
| 84 | #define SHRINK_FULLNESS_FACTOR 2 |
| 85 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 86 | static void maybe_shrink(grpc_alarm_heap *heap) { |
| 87 | if (heap->alarm_count >= 8 && |
| 88 | heap->alarm_count <= heap->alarm_capacity / SHRINK_FULLNESS_FACTOR / 2) { |
| 89 | heap->alarm_capacity = heap->alarm_count * SHRINK_FULLNESS_FACTOR; |
| 90 | heap->alarms = |
| 91 | gpr_realloc(heap->alarms, heap->alarm_capacity * sizeof(grpc_alarm *)); |
| 92 | } |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 95 | static void note_changed_priority(grpc_alarm_heap *heap, grpc_alarm *alarm) { |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 96 | gpr_uint32 i = alarm->heap_index; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 97 | gpr_uint32 parent = (gpr_uint32)(((int)i - 1) / 2); |
| 98 | if (gpr_time_cmp(heap->alarms[parent]->deadline, alarm->deadline) < 0) { |
| 99 | adjust_upwards(heap->alarms, i, alarm); |
| 100 | } else { |
| 101 | adjust_downwards(heap->alarms, i, heap->alarm_count, alarm); |
| 102 | } |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 105 | void grpc_alarm_heap_init(grpc_alarm_heap *heap) { |
| 106 | memset(heap, 0, sizeof(*heap)); |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 109 | void grpc_alarm_heap_destroy(grpc_alarm_heap *heap) { gpr_free(heap->alarms); } |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 110 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 111 | int grpc_alarm_heap_add(grpc_alarm_heap *heap, grpc_alarm *alarm) { |
| 112 | if (heap->alarm_count == heap->alarm_capacity) { |
| 113 | heap->alarm_capacity = |
| 114 | GPR_MAX(heap->alarm_capacity + 1, heap->alarm_capacity * 3 / 2); |
| 115 | heap->alarms = |
| 116 | gpr_realloc(heap->alarms, heap->alarm_capacity * sizeof(grpc_alarm *)); |
| 117 | } |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 118 | alarm->heap_index = heap->alarm_count; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 119 | adjust_upwards(heap->alarms, heap->alarm_count, alarm); |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 120 | heap->alarm_count++; |
| 121 | return alarm->heap_index == 0; |
| 122 | } |
| 123 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 124 | void grpc_alarm_heap_remove(grpc_alarm_heap *heap, grpc_alarm *alarm) { |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 125 | gpr_uint32 i = alarm->heap_index; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 126 | if (i == heap->alarm_count - 1) { |
| 127 | heap->alarm_count--; |
| 128 | maybe_shrink(heap); |
| 129 | return; |
| 130 | } |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 131 | heap->alarms[i] = heap->alarms[heap->alarm_count - 1]; |
| 132 | heap->alarms[i]->heap_index = i; |
| 133 | heap->alarm_count--; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 134 | maybe_shrink(heap); |
| 135 | note_changed_priority(heap, heap->alarms[i]); |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 138 | int grpc_alarm_heap_is_empty(grpc_alarm_heap *heap) { |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 139 | return heap->alarm_count == 0; |
| 140 | } |
| 141 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 142 | grpc_alarm *grpc_alarm_heap_top(grpc_alarm_heap *heap) { |
ctiller | 3bf466f | 2014-12-19 16:21:57 -0800 | [diff] [blame] | 143 | return heap->alarms[0]; |
| 144 | } |
| 145 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame^] | 146 | void grpc_alarm_heap_pop(grpc_alarm_heap *heap) { |
| 147 | grpc_alarm_heap_remove(heap, grpc_alarm_heap_top(heap)); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 148 | } |