android_system_core/libunwindstack/include/unwindstack/Maps.h
Christopher Ferris 60521c7d52 Speed up map creation.
- Rewrite the Maps::Parse to use open, and a buffer on the stack.
- Rewrite the line parser away from sscanf. The current way sscanf
  is used does not catch many malformed lines. In addition, this
  new version improves performance by 50% over sscanf on sailfish.
- Add a lot of unit tests for the parser to make sure there are
  no problems. In addition, add a special line that was not rejected
  with the previous version of the code.
- Add new accessor to get the map at a particular index.
- Add a backtrace benchmark for map creation for both new and old.

This cl results in ~5% speedup and makes the new unwinder map creation
about the same for 64 bit. It's still a bit slower, but not by much.
On 32 bit, we are still about 5% slower than the old creation method,
though.

Bug: 23762183

Test: libunwindstack unit tests pass. Ran the new benchmarks.
Change-Id: Id4431e539f400984e6fad62153fdf4152d518322
2017-08-23 15:43:39 -07:00

113 lines
2.6 KiB
C++

/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _LIBUNWINDSTACK_MAPS_H
#define _LIBUNWINDSTACK_MAPS_H
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <unwindstack/MapInfo.h>
namespace unwindstack {
// Special flag to indicate a map is in /dev/. However, a map in
// /dev/ashmem/... does not set this flag.
static constexpr int MAPS_FLAGS_DEVICE_MAP = 0x8000;
class Maps {
public:
Maps() = default;
virtual ~Maps();
MapInfo* Find(uint64_t pc);
virtual bool Parse();
virtual const std::string GetMapsFile() const { return ""; }
typedef std::vector<MapInfo>::iterator iterator;
iterator begin() { return maps_.begin(); }
iterator end() { return maps_.end(); }
typedef std::vector<MapInfo>::const_iterator const_iterator;
const_iterator begin() const { return maps_.begin(); }
const_iterator end() const { return maps_.end(); }
size_t Total() { return maps_.size(); }
MapInfo* Get(size_t index) {
if (index >= maps_.size()) return nullptr;
return &maps_[index];
}
protected:
std::vector<MapInfo> maps_;
};
class RemoteMaps : public Maps {
public:
RemoteMaps(pid_t pid) : pid_(pid) {}
virtual ~RemoteMaps() = default;
virtual const std::string GetMapsFile() const override;
private:
pid_t pid_;
};
class LocalMaps : public RemoteMaps {
public:
LocalMaps() : RemoteMaps(getpid()) {}
virtual ~LocalMaps() = default;
};
class BufferMaps : public Maps {
public:
BufferMaps(const char* buffer) : buffer_(buffer) {}
virtual ~BufferMaps() = default;
bool Parse() override;
private:
const char* buffer_;
};
class FileMaps : public Maps {
public:
FileMaps(const std::string& file) : file_(file) {}
virtual ~FileMaps() = default;
const std::string GetMapsFile() const override { return file_; }
protected:
const std::string file_;
};
class OfflineMaps : public FileMaps {
public:
OfflineMaps(const std::string& file) : FileMaps(file) {}
virtual ~OfflineMaps() = default;
bool Parse() override;
};
} // namespace unwindstack
#endif // _LIBUNWINDSTACK_MAPS_H