]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | package org.apache.arrow.memory; | |
19 | ||
20 | import java.util.ArrayDeque; | |
21 | import java.util.Deque; | |
22 | ||
23 | /** | |
24 | * Captures details of allocation for each accountant in the hierarchical chain. | |
25 | */ | |
26 | public class AllocationOutcomeDetails { | |
27 | Deque<Entry> allocEntries; | |
28 | ||
29 | AllocationOutcomeDetails() { | |
30 | allocEntries = new ArrayDeque<>(); | |
31 | } | |
32 | ||
33 | void pushEntry(Accountant accountant, long totalUsedBeforeAllocation, long requestedSize, | |
34 | long allocatedSize, boolean allocationFailed) { | |
35 | ||
36 | Entry top = allocEntries.peekLast(); | |
37 | if (top != null && top.allocationFailed) { | |
38 | // if the allocation has already failed, stop saving the entries. | |
39 | return; | |
40 | } | |
41 | ||
42 | allocEntries.addLast(new Entry(accountant, totalUsedBeforeAllocation, requestedSize, | |
43 | allocatedSize, allocationFailed)); | |
44 | } | |
45 | ||
46 | /** | |
47 | * Get the allocator that caused the failure. | |
48 | * @return the allocator that caused failure, null if there was no failure. | |
49 | */ | |
50 | public BufferAllocator getFailedAllocator() { | |
51 | Entry top = allocEntries.peekLast(); | |
52 | if (top != null && top.allocationFailed && (top.accountant instanceof BufferAllocator)) { | |
53 | return (BufferAllocator) top.accountant; | |
54 | } else { | |
55 | return null; | |
56 | } | |
57 | } | |
58 | ||
59 | @Override | |
60 | public String toString() { | |
61 | StringBuilder sb = new StringBuilder(); | |
62 | sb.append("Allocation outcome details:\n"); | |
63 | allocEntries.forEach(sb::append); | |
64 | return sb.toString(); | |
65 | } | |
66 | ||
67 | /** | |
68 | * Outcome of the allocation request at one accountant in the hierarchy. | |
69 | */ | |
70 | public static class Entry { | |
71 | private final Accountant accountant; | |
72 | ||
73 | // Remember allocator attributes at the time of the request. | |
74 | private final long limit; | |
75 | private final long used; | |
76 | ||
77 | // allocation outcome | |
78 | private final long requestedSize; | |
79 | private final long allocatedSize; | |
80 | private final boolean allocationFailed; | |
81 | ||
82 | Entry(Accountant accountant, long totalUsedBeforeAllocation, long requestedSize, | |
83 | long allocatedSize, boolean allocationFailed) { | |
84 | this.accountant = accountant; | |
85 | this.limit = accountant.getLimit(); | |
86 | this.used = totalUsedBeforeAllocation; | |
87 | ||
88 | this.requestedSize = requestedSize; | |
89 | this.allocatedSize = allocatedSize; | |
90 | this.allocationFailed = allocationFailed; | |
91 | } | |
92 | ||
93 | public Accountant getAccountant() { | |
94 | return accountant; | |
95 | } | |
96 | ||
97 | public long getLimit() { | |
98 | return limit; | |
99 | } | |
100 | ||
101 | public long getUsed() { | |
102 | return used; | |
103 | } | |
104 | ||
105 | public long getRequestedSize() { | |
106 | return requestedSize; | |
107 | } | |
108 | ||
109 | public long getAllocatedSize() { | |
110 | return allocatedSize; | |
111 | } | |
112 | ||
113 | public boolean isAllocationFailed() { | |
114 | return allocationFailed; | |
115 | } | |
116 | ||
117 | @Override | |
118 | public String toString() { | |
119 | return new StringBuilder() | |
120 | .append("allocator[" + accountant.getName() + "]") | |
121 | .append(" reservation: " + accountant.getInitReservation()) | |
122 | .append(" limit: " + limit) | |
123 | .append(" used: " + used) | |
124 | .append(" requestedSize: " + requestedSize) | |
125 | .append(" allocatedSize: " + allocatedSize) | |
126 | .append(" localAllocationStatus: " + (allocationFailed ? "fail" : "success")) | |
127 | .append("\n") | |
128 | .toString(); | |
129 | } | |
130 | } | |
131 | ||
132 | } |