blob: be39a589964c28fe4954136ea09612ff2896c75e [file] [log] [blame]
Ian Rogers1d54e732013-05-02 21:10:01 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
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 "image_space.h"
18
Brian Carlstrom56d947f2013-07-15 13:14:23 -070019#include <sys/types.h>
20#include <sys/wait.h>
21
22#include "base/stl_util.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include "base/unix_file/fd_file.h"
24#include "gc/accounting/space_bitmap-inl.h"
25#include "mirror/abstract_method.h"
26#include "mirror/class-inl.h"
27#include "mirror/object-inl.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070028#include "oat_file.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "os.h"
30#include "runtime.h"
31#include "space-inl.h"
32#include "utils.h"
33
34namespace art {
35namespace gc {
36namespace space {
37
38size_t ImageSpace::bitmap_index_ = 0;
39
40ImageSpace::ImageSpace(const std::string& name, MemMap* mem_map)
41: MemMapSpace(name, mem_map, mem_map->Size(), kGcRetentionPolicyNeverCollect) {
42 const size_t bitmap_index = bitmap_index_++;
43 live_bitmap_.reset(accounting::SpaceBitmap::Create(
44 StringPrintf("imagespace %s live-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
45 Begin(), Capacity()));
46 DCHECK(live_bitmap_.get() != NULL) << "could not create imagespace live bitmap #" << bitmap_index;
47}
48
Brian Carlstrom56d947f2013-07-15 13:14:23 -070049static bool GenerateImage(const std::string& image_file_name) {
50 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
51 std::vector<std::string> boot_class_path;
52 Split(boot_class_path_string, ':', boot_class_path);
53 if (boot_class_path.empty()) {
54 LOG(FATAL) << "Failed to generate image because no boot class path specified";
55 }
56
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070057 std::vector<std::string> arg_vector;
Brian Carlstrom56d947f2013-07-15 13:14:23 -070058
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070059 const char* dex2oat = GetAndroidRoot();
60 std::string dex2oat_string(dex2oat);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070061 dex2oat_string += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat");
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070062 arg_vector.push_back(dex2oat_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070063
64 std::string image_option_string("--image=");
65 image_option_string += image_file_name;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070066 arg_vector.push_back(image_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070067
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070068 arg_vector.push_back("--runtime-arg");
69 arg_vector.push_back("-Xms64m");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070070
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070071 arg_vector.push_back("--runtime-arg");
72 arg_vector.push_back("-Xmx64m");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070073
74 for (size_t i = 0; i < boot_class_path.size(); i++) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070075 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070076 }
77
78 std::string oat_file_option_string("--oat-file=");
79 oat_file_option_string += image_file_name;
80 oat_file_option_string.erase(oat_file_option_string.size() - 3);
81 oat_file_option_string += "oat";
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070082 arg_vector.push_back(oat_file_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070083
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070084 arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS));
Brian Carlstrom56d947f2013-07-15 13:14:23 -070085
86 if (kIsTargetBuild) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070087 arg_vector.push_back("--image-classes-zip=/system/framework/framework.jar");
88 arg_vector.push_back("--image-classes=preloaded-classes");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070089 } else {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070090 arg_vector.push_back("--host");
Brian Carlstrom56d947f2013-07-15 13:14:23 -070091 }
92
93 std::string command_line(Join(arg_vector, ' '));
94 LOG(INFO) << "GenerateImage: " << command_line;
95
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -070096 // Convert the args to char pointers.
97 std::vector<char*> char_args;
98 for (std::vector<std::string>::iterator it = arg_vector.begin(); it != arg_vector.end();
99 ++it) {
100 char_args.push_back(const_cast<char*>(it->c_str()));
101 }
102 char_args.push_back(NULL);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700103
104 // fork and exec dex2oat
105 pid_t pid = fork();
106 if (pid == 0) {
107 // no allocation allowed between fork and exec
108
109 // change process groups, so we don't get reaped by ProcessManager
110 setpgid(0, 0);
111
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700112 execv(dex2oat, &char_args[0]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700113
114 PLOG(FATAL) << "execv(" << dex2oat << ") failed";
115 return false;
116 } else {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700117 if (pid == -1) {
118 PLOG(ERROR) << "fork failed";
119 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700120
121 // wait for dex2oat to finish
122 int status;
123 pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
124 if (got_pid != pid) {
125 PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid;
126 return false;
127 }
128 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
129 LOG(ERROR) << dex2oat << " failed: " << command_line;
130 return false;
131 }
132 }
133 return true;
134}
135
136ImageSpace* ImageSpace::Create(const std::string& original_image_file_name) {
137 if (OS::FileExists(original_image_file_name.c_str())) {
138 // If the /system file exists, it should be up-to-date, don't try to generate
139 return space::ImageSpace::Init(original_image_file_name, false);
140 }
141 // If the /system file didn't exist, we need to use one from the dalvik-cache.
142 // If the cache file exists, try to open, but if it fails, regenerate.
143 // If it does not exist, generate.
144 std::string image_file_name(GetDalvikCacheFilenameOrDie(original_image_file_name));
145 if (OS::FileExists(image_file_name.c_str())) {
146 space::ImageSpace* image_space = space::ImageSpace::Init(image_file_name, true);
147 if (image_space != NULL) {
148 return image_space;
149 }
150 }
151 CHECK(GenerateImage(image_file_name)) << "Failed to generate image: " << image_file_name;
152 return space::ImageSpace::Init(image_file_name, true);
153}
154
155ImageSpace* ImageSpace::Init(const std::string& image_file_name, bool validate_oat_file) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700156 CHECK(!image_file_name.empty());
157
158 uint64_t start_time = 0;
159 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
160 start_time = NanoTime();
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700161 LOG(INFO) << "ImageSpace::Init entering image_file_name=" << image_file_name;
Ian Rogers1d54e732013-05-02 21:10:01 -0700162 }
163
164 UniquePtr<File> file(OS::OpenFile(image_file_name.c_str(), false));
165 if (file.get() == NULL) {
166 LOG(ERROR) << "Failed to open " << image_file_name;
167 return NULL;
168 }
169 ImageHeader image_header;
170 bool success = file->ReadFully(&image_header, sizeof(image_header));
171 if (!success || !image_header.IsValid()) {
172 LOG(ERROR) << "Invalid image header " << image_file_name;
173 return NULL;
174 }
175 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
176 file->GetLength(),
177 PROT_READ | PROT_WRITE,
178 MAP_PRIVATE | MAP_FIXED,
179 file->Fd(),
180 0,
181 false));
182 if (map.get() == NULL) {
183 LOG(ERROR) << "Failed to map " << image_file_name;
184 return NULL;
185 }
186 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
187 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
188
189 Runtime* runtime = Runtime::Current();
190 mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
191 runtime->SetResolutionMethod(down_cast<mirror::AbstractMethod*>(resolution_method));
192
193 mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
194 runtime->SetCalleeSaveMethod(down_cast<mirror::AbstractMethod*>(callee_save_method), Runtime::kSaveAll);
195 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
196 runtime->SetCalleeSaveMethod(down_cast<mirror::AbstractMethod*>(callee_save_method), Runtime::kRefsOnly);
197 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
198 runtime->SetCalleeSaveMethod(down_cast<mirror::AbstractMethod*>(callee_save_method), Runtime::kRefsAndArgs);
199
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700200 UniquePtr<ImageSpace> space(new ImageSpace(image_file_name, map.release()));
201
202 space->oat_file_.reset(space->OpenOatFile());
203 if (space->oat_file_.get() == NULL) {
204 LOG(ERROR) << "Failed to open oat file for image: " << image_file_name;
205 return NULL;
Ian Rogers1d54e732013-05-02 21:10:01 -0700206 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700207
208 if (validate_oat_file && !space->ValidateOatFile()) {
209 LOG(WARNING) << "Failed to validate oat file for image: " << image_file_name;
210 return NULL;
211 }
212
213 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
214 LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
215 << ") " << *space.get();
216 }
217 return space.release();
218}
219
220OatFile* ImageSpace::OpenOatFile() const {
221 const Runtime* runtime = Runtime::Current();
222 const ImageHeader& image_header = GetImageHeader();
223 // Grab location but don't use Object::AsString as we haven't yet initialized the roots to
224 // check the down cast
225 mirror::String* oat_location =
226 down_cast<mirror::String*>(image_header.GetImageRoot(ImageHeader::kOatLocation));
227 std::string oat_filename;
228 oat_filename += runtime->GetHostPrefix();
229 oat_filename += oat_location->ToModifiedUtf8();
230 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(),
231 !Runtime::Current()->IsCompiler());
232 if (oat_file == NULL) {
233 LOG(ERROR) << "Failed to open oat file " << oat_filename << " referenced from image.";
234 return NULL;
235 }
236 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
237 uint32_t image_oat_checksum = image_header.GetOatChecksum();
238 if (oat_checksum != image_oat_checksum) {
239 LOG(ERROR) << "Failed to match oat file checksum " << std::hex << oat_checksum
240 << " to expected oat checksum " << std::hex << image_oat_checksum
241 << " in image";
242 return NULL;
243 }
244 return oat_file;
245}
246
247bool ImageSpace::ValidateOatFile() const {
248 CHECK(oat_file_.get() != NULL);
249 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
250 for (size_t i = 0; i < oat_dex_files.size(); i++) {
251 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
252 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
253 uint32_t dex_file_location_checksum;
254 if (!DexFile::GetChecksum(dex_file_location.c_str(), dex_file_location_checksum)) {
255 LOG(WARNING) << "ValidateOatFile could not find checksum for " << dex_file_location;
256 return false;
257 }
258 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
259 LOG(WARNING) << "ValidateOatFile found checksum mismatch between oat file "
260 << oat_file_->GetLocation() << " and dex file " << dex_file_location
261 << " (" << oat_dex_file->GetDexFileLocationChecksum() << " != "
262 << dex_file_location_checksum << ")";
263 return false;
264 }
265 }
266 return true;
267}
268
269OatFile& ImageSpace::ReleaseOatFile() {
270 CHECK(oat_file_.get() != NULL);
271 return *oat_file_.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700272}
273
274void ImageSpace::RecordImageAllocations(accounting::SpaceBitmap* live_bitmap) const {
275 uint64_t start_time = 0;
276 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
277 LOG(INFO) << "ImageSpace::RecordImageAllocations entering";
278 start_time = NanoTime();
279 }
280 DCHECK(!Runtime::Current()->IsStarted());
281 CHECK(live_bitmap != NULL);
282 byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
283 byte* end = End();
284 while (current < end) {
285 DCHECK_ALIGNED(current, kObjectAlignment);
286 const mirror::Object* obj = reinterpret_cast<const mirror::Object*>(current);
287 live_bitmap->Set(obj);
288 current += RoundUp(obj->SizeOf(), kObjectAlignment);
289 }
290 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
291 LOG(INFO) << "ImageSpace::RecordImageAllocations exiting ("
292 << PrettyDuration(NanoTime() - start_time) << ")";
293 }
294}
295
296void ImageSpace::Dump(std::ostream& os) const {
297 os << GetType()
298 << "begin=" << reinterpret_cast<void*>(Begin())
299 << ",end=" << reinterpret_cast<void*>(End())
300 << ",size=" << PrettySize(Size())
301 << ",name=\"" << GetName() << "\"]";
302}
303
304} // namespace space
305} // namespace gc
306} // namespace art