Fix Idle count
[samplevnf.git] / VNFs / DPPD-PROX / tools / flow_extract / memreader.cpp
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #include <cstdlib>
18
19 #include "memreader.hpp"
20 #include "mappedfile.hpp"
21 #include "stream3.hpp"
22
23 MemReader::MemReader(MappedFile *file, const vector<size_t> &offsets)
24 {
25         initRanges(file->getMapBeg(), file->getMapEnd(), offsets);
26 }
27
28 bool MemReader::read(Stream3 *stream)
29 {
30         if (ranges.empty())
31                 return false;
32
33         readStream(stream, getLowestID());
34         removeEmptyRanges();
35         return true;
36 }
37
38 uint32_t MemReader::getLowestID() const
39 {
40         uint32_t lowestID = UINT32_MAX;
41         uint32_t rangeID;
42
43         for (size_t i = 0; i < ranges.size(); ++i) {
44                 rangeID = Stream3::getIDFromMem(ranges[i].first);
45                 if (rangeID < lowestID)
46                         lowestID = rangeID;
47         }
48         return lowestID;
49 }
50
51 void MemReader::readStream(Stream3 *stream, uint32_t id)
52 {
53         stream->removeAllPackets();
54         stream->setID(id);
55         
56         size_t len = 0;
57         for (size_t i = 0; i < ranges.size(); ++i) {
58                 if (Stream3::getIDFromMem(ranges[i].first) == id) {
59                         stream->addFromMemory(ranges[i].first, &len);
60                         ranges[i].first += len;
61                 }
62         }
63 }
64
65 void MemReader::removeEmptyRanges()
66 {
67         vector<pair <uint8_t *, uint8_t *> > original = ranges;
68         size_t destinationIdx = 0;
69
70         for (size_t i = 0; i < original.size(); ++i) {
71                 if (original[i].first < original[i].second)
72                         ranges[destinationIdx++] = original[i];
73         }
74         ranges.resize(destinationIdx);
75 }
76
77 void MemReader::initRanges(uint8_t *begin, uint8_t *end, const vector<size_t> &offsets)
78 {
79         ranges.resize(offsets.size());
80
81         totalLength = 0;
82         for (size_t i = 0; i < offsets.size(); ++i) {
83                 ranges[i].first = begin + offsets[i];
84                 if (i != offsets.size() - 1)
85                         ranges[i].second = begin + offsets[i + 1];
86                 else
87                         ranges[i].second = end;
88                 totalLength += ranges[i].second - ranges[i].first;
89         }
90         removeEmptyRanges();
91 }
92
93 size_t MemReader::getRangeLengths() const
94 {
95         size_t total = 0;
96
97         for (size_t i = 0; i < ranges.size(); ++i) {
98                 total += ranges[i].second - ranges[i].first;
99         }
100         return total;
101 }
102
103 size_t MemReader::consumed() const
104 {
105         return totalLength - getRangeLengths();
106 }