1695b41eeSopenharmony_ci// Copyright 2012 Google Inc. All Rights Reserved. 2695b41eeSopenharmony_ci// 3695b41eeSopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 4695b41eeSopenharmony_ci// you may not use this file except in compliance with the License. 5695b41eeSopenharmony_ci// You may obtain a copy of the License at 6695b41eeSopenharmony_ci// 7695b41eeSopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 8695b41eeSopenharmony_ci// 9695b41eeSopenharmony_ci// Unless required by applicable law or agreed to in writing, software 10695b41eeSopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 11695b41eeSopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12695b41eeSopenharmony_ci// See the License for the specific language governing permissions and 13695b41eeSopenharmony_ci// limitations under the License. 14695b41eeSopenharmony_ci 15695b41eeSopenharmony_ci#ifndef NINJA_DEPS_LOG_H_ 16695b41eeSopenharmony_ci#define NINJA_DEPS_LOG_H_ 17695b41eeSopenharmony_ci 18695b41eeSopenharmony_ci#include <string> 19695b41eeSopenharmony_ci#include <vector> 20695b41eeSopenharmony_ci 21695b41eeSopenharmony_ci#include <stdio.h> 22695b41eeSopenharmony_ci 23695b41eeSopenharmony_ci#include "load_status.h" 24695b41eeSopenharmony_ci#include "timestamp.h" 25695b41eeSopenharmony_ci 26695b41eeSopenharmony_cistruct Node; 27695b41eeSopenharmony_cistruct State; 28695b41eeSopenharmony_ci 29695b41eeSopenharmony_ci/// As build commands run they can output extra dependency information 30695b41eeSopenharmony_ci/// (e.g. header dependencies for C source) dynamically. DepsLog collects 31695b41eeSopenharmony_ci/// that information at build time and uses it for subsequent builds. 32695b41eeSopenharmony_ci/// 33695b41eeSopenharmony_ci/// The on-disk format is based on two primary design constraints: 34695b41eeSopenharmony_ci/// - it must be written to as a stream (during the build, which may be 35695b41eeSopenharmony_ci/// interrupted); 36695b41eeSopenharmony_ci/// - it can be read all at once on startup. (Alternative designs, where 37695b41eeSopenharmony_ci/// it contains indexing information, were considered and discarded as 38695b41eeSopenharmony_ci/// too complicated to implement; if the file is small than reading it 39695b41eeSopenharmony_ci/// fully on startup is acceptable.) 40695b41eeSopenharmony_ci/// Here are some stats from the Windows Chrome dependency files, to 41695b41eeSopenharmony_ci/// help guide the design space. The total text in the files sums to 42695b41eeSopenharmony_ci/// 90mb so some compression is warranted to keep load-time fast. 43695b41eeSopenharmony_ci/// There's about 10k files worth of dependencies that reference about 44695b41eeSopenharmony_ci/// 40k total paths totalling 2mb of unique strings. 45695b41eeSopenharmony_ci/// 46695b41eeSopenharmony_ci/// Based on these stats, here's the current design. 47695b41eeSopenharmony_ci/// The file is structured as version header followed by a sequence of records. 48695b41eeSopenharmony_ci/// Each record is either a path string or a dependency list. 49695b41eeSopenharmony_ci/// Numbering the path strings in file order gives them dense integer ids. 50695b41eeSopenharmony_ci/// A dependency list maps an output id to a list of input ids. 51695b41eeSopenharmony_ci/// 52695b41eeSopenharmony_ci/// Concretely, a record is: 53695b41eeSopenharmony_ci/// four bytes record length, high bit indicates record type 54695b41eeSopenharmony_ci/// (but max record sizes are capped at 512kB) 55695b41eeSopenharmony_ci/// path records contain the string name of the path, followed by up to 3 56695b41eeSopenharmony_ci/// padding bytes to align on 4 byte boundaries, followed by the 57695b41eeSopenharmony_ci/// one's complement of the expected index of the record (to detect 58695b41eeSopenharmony_ci/// concurrent writes of multiple ninja processes to the log). 59695b41eeSopenharmony_ci/// dependency records are an array of 4-byte integers 60695b41eeSopenharmony_ci/// [output path id, 61695b41eeSopenharmony_ci/// output path mtime (lower 4 bytes), output path mtime (upper 4 bytes), 62695b41eeSopenharmony_ci/// input path id, input path id...] 63695b41eeSopenharmony_ci/// (The mtime is compared against the on-disk output path mtime 64695b41eeSopenharmony_ci/// to verify the stored data is up-to-date.) 65695b41eeSopenharmony_ci/// If two records reference the same output the latter one in the file 66695b41eeSopenharmony_ci/// wins, allowing updates to just be appended to the file. A separate 67695b41eeSopenharmony_ci/// repacking step can run occasionally to remove dead records. 68695b41eeSopenharmony_cistruct DepsLog { 69695b41eeSopenharmony_ci DepsLog() : needs_recompaction_(false), file_(NULL) {} 70695b41eeSopenharmony_ci ~DepsLog(); 71695b41eeSopenharmony_ci 72695b41eeSopenharmony_ci // Writing (build-time) interface. 73695b41eeSopenharmony_ci bool OpenForWrite(const std::string& path, std::string* err); 74695b41eeSopenharmony_ci bool RecordDeps(Node* node, TimeStamp mtime, const std::vector<Node*>& nodes); 75695b41eeSopenharmony_ci bool RecordDeps(Node* node, TimeStamp mtime, int node_count, Node** nodes); 76695b41eeSopenharmony_ci void Close(); 77695b41eeSopenharmony_ci 78695b41eeSopenharmony_ci // Reading (startup-time) interface. 79695b41eeSopenharmony_ci struct Deps { 80695b41eeSopenharmony_ci Deps(int64_t mtime, int node_count) 81695b41eeSopenharmony_ci : mtime(mtime), node_count(node_count), nodes(new Node*[node_count]) {} 82695b41eeSopenharmony_ci ~Deps() { delete [] nodes; } 83695b41eeSopenharmony_ci TimeStamp mtime; 84695b41eeSopenharmony_ci int node_count; 85695b41eeSopenharmony_ci Node** nodes; 86695b41eeSopenharmony_ci }; 87695b41eeSopenharmony_ci LoadStatus Load(const std::string& path, State* state, std::string* err); 88695b41eeSopenharmony_ci Deps* GetDeps(Node* node); 89695b41eeSopenharmony_ci Node* GetFirstReverseDepsNode(Node* node); 90695b41eeSopenharmony_ci 91695b41eeSopenharmony_ci /// Rewrite the known log entries, throwing away old data. 92695b41eeSopenharmony_ci bool Recompact(const std::string& path, std::string* err); 93695b41eeSopenharmony_ci 94695b41eeSopenharmony_ci /// Returns if the deps entry for a node is still reachable from the manifest. 95695b41eeSopenharmony_ci /// 96695b41eeSopenharmony_ci /// The deps log can contain deps entries for files that were built in the 97695b41eeSopenharmony_ci /// past but are no longer part of the manifest. This function returns if 98695b41eeSopenharmony_ci /// this is the case for a given node. This function is slow, don't call 99695b41eeSopenharmony_ci /// it from code that runs on every build. 100695b41eeSopenharmony_ci static bool IsDepsEntryLiveFor(const Node* node); 101695b41eeSopenharmony_ci 102695b41eeSopenharmony_ci /// Used for tests. 103695b41eeSopenharmony_ci const std::vector<Node*>& nodes() const { return nodes_; } 104695b41eeSopenharmony_ci const std::vector<Deps*>& deps() const { return deps_; } 105695b41eeSopenharmony_ci 106695b41eeSopenharmony_ci private: 107695b41eeSopenharmony_ci // Updates the in-memory representation. Takes ownership of |deps|. 108695b41eeSopenharmony_ci // Returns true if a prior deps record was deleted. 109695b41eeSopenharmony_ci bool UpdateDeps(int out_id, Deps* deps); 110695b41eeSopenharmony_ci // Write a node name record, assigning it an id. 111695b41eeSopenharmony_ci bool RecordId(Node* node); 112695b41eeSopenharmony_ci 113695b41eeSopenharmony_ci /// Should be called before using file_. When false is returned, errno will 114695b41eeSopenharmony_ci /// be set. 115695b41eeSopenharmony_ci bool OpenForWriteIfNeeded(); 116695b41eeSopenharmony_ci 117695b41eeSopenharmony_ci bool needs_recompaction_; 118695b41eeSopenharmony_ci FILE* file_; 119695b41eeSopenharmony_ci std::string file_path_; 120695b41eeSopenharmony_ci 121695b41eeSopenharmony_ci /// Maps id -> Node. 122695b41eeSopenharmony_ci std::vector<Node*> nodes_; 123695b41eeSopenharmony_ci /// Maps id -> deps of that id. 124695b41eeSopenharmony_ci std::vector<Deps*> deps_; 125695b41eeSopenharmony_ci 126695b41eeSopenharmony_ci friend struct DepsLogTest; 127695b41eeSopenharmony_ci}; 128695b41eeSopenharmony_ci 129695b41eeSopenharmony_ci#endif // NINJA_DEPS_LOG_H_ 130