OSDN Git Service

ART: Check for more low-space situations in zygote
[android-x86/art.git] / runtime / gc / space / image_space_fs.h
1 /*
2  * Copyright (C) 2016 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 #ifndef ART_RUNTIME_GC_SPACE_IMAGE_SPACE_FS_H_
18 #define ART_RUNTIME_GC_SPACE_IMAGE_SPACE_FS_H_
19
20 #include <dirent.h>
21 #include <dlfcn.h>
22
23 #include "base/logging.h"
24 #include "base/macros.h"
25 #include "base/stringprintf.h"
26 #include "base/unix_file/fd_file.h"
27 #include "globals.h"
28 #include "os.h"
29 #include "runtime.h"
30 #include "utils.h"
31
32 namespace art {
33 namespace gc {
34 namespace space {
35
36 // This file contains helper code for ImageSpace. It has most of the file-system
37 // related code, including handling A/B OTA.
38
39 namespace impl {
40
41 // Delete the directory and its (regular or link) contents. If the recurse flag is true, delete
42 // sub-directories recursively.
43 static void DeleteDirectoryContents(const std::string& dir, bool recurse) {
44   if (!OS::DirectoryExists(dir.c_str())) {
45     return;
46   }
47   DIR* c_dir = opendir(dir.c_str());
48   if (c_dir == nullptr) {
49     PLOG(WARNING) << "Unable to open " << dir << " to delete it's contents";
50     return;
51   }
52
53   for (struct dirent* de = readdir(c_dir); de != nullptr; de = readdir(c_dir)) {
54     const char* name = de->d_name;
55     if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
56       continue;
57     }
58     // We only want to delete regular files and symbolic links.
59     std::string file = StringPrintf("%s/%s", dir.c_str(), name);
60     if (de->d_type != DT_REG && de->d_type != DT_LNK) {
61       if (de->d_type == DT_DIR) {
62         if (recurse) {
63           DeleteDirectoryContents(file, recurse);
64           // Try to rmdir the directory.
65           if (rmdir(file.c_str()) != 0) {
66             PLOG(ERROR) << "Unable to rmdir " << file;
67           }
68         }
69       } else {
70         LOG(WARNING) << "Unexpected file type of " << std::hex << de->d_type << " encountered.";
71       }
72     } else {
73       // Try to unlink the file.
74       if (unlink(file.c_str()) != 0) {
75         PLOG(ERROR) << "Unable to unlink " << file;
76       }
77     }
78   }
79   CHECK_EQ(0, closedir(c_dir)) << "Unable to close directory.";
80 }
81
82 }  // namespace impl
83
84
85 // We are relocating or generating the core image. We should get rid of everything. It is all
86 // out-of-date. We also don't really care if this fails since it is just a convenience.
87 // Adapted from prune_dex_cache(const char* subdir) in frameworks/native/cmds/installd/commands.c
88 // Note this should only be used during first boot.
89 static void PruneDalvikCache(InstructionSet isa) {
90   CHECK_NE(isa, kNone);
91   // Prune the base /data/dalvik-cache.
92   impl::DeleteDirectoryContents(GetDalvikCacheOrDie(".", false), false);
93   // Prune /data/dalvik-cache/<isa>.
94   impl::DeleteDirectoryContents(GetDalvikCacheOrDie(GetInstructionSetString(isa), false), false);
95
96   // Be defensive. There should be a runtime created here, but this may be called in a test.
97   if (Runtime::Current() != nullptr) {
98     Runtime::Current()->SetPrunedDalvikCache(true);
99   }
100 }
101
102 // We write out an empty file to the zygote's ISA specific cache dir at the start of
103 // every zygote boot and delete it when the boot completes. If we find a file already
104 // present, it usually means the boot didn't complete. We wipe the entire dalvik
105 // cache if that's the case.
106 static void MarkZygoteStart(const InstructionSet isa, const uint32_t max_failed_boots) {
107   const std::string isa_subdir = GetDalvikCacheOrDie(GetInstructionSetString(isa), false);
108   const std::string boot_marker = isa_subdir + "/.booting";
109   const char* file_name = boot_marker.c_str();
110
111   uint32_t num_failed_boots = 0;
112   std::unique_ptr<File> file(OS::OpenFileReadWrite(file_name));
113   if (file.get() == nullptr) {
114     file.reset(OS::CreateEmptyFile(file_name));
115
116     if (file.get() == nullptr) {
117       int saved_errno = errno;
118       PLOG(WARNING) << "Failed to create boot marker.";
119       if (saved_errno != ENOSPC) {
120         return;
121       }
122
123       LOG(WARNING) << "Pruning dalvik cache because of low-memory situation.";
124       impl::DeleteDirectoryContents(isa_subdir, false);
125
126       // Try once more.
127       file.reset(OS::OpenFileReadWrite(file_name));
128       if (file == nullptr) {
129         PLOG(WARNING) << "Failed to create boot marker.";
130         return;
131       }
132     }
133   } else {
134     if (!file->ReadFully(&num_failed_boots, sizeof(num_failed_boots))) {
135       PLOG(WARNING) << "Failed to read boot marker.";
136       file->Erase();
137       return;
138     }
139   }
140
141   if (max_failed_boots != 0 && num_failed_boots > max_failed_boots) {
142     LOG(WARNING) << "Incomplete boot detected. Pruning dalvik cache";
143     impl::DeleteDirectoryContents(isa_subdir, false);
144   }
145
146   ++num_failed_boots;
147   VLOG(startup) << "Number of failed boots on : " << boot_marker << " = " << num_failed_boots;
148
149   if (lseek(file->Fd(), 0, SEEK_SET) == -1) {
150     PLOG(WARNING) << "Failed to write boot marker.";
151     file->Erase();
152     return;
153   }
154
155   if (!file->WriteFully(&num_failed_boots, sizeof(num_failed_boots))) {
156     PLOG(WARNING) << "Failed to write boot marker.";
157     file->Erase();
158     return;
159   }
160
161   if (file->FlushCloseOrErase() != 0) {
162     PLOG(WARNING) << "Failed to flush boot marker.";
163   }
164 }
165
166 }  // namespace space
167 }  // namespace gc
168 }  // namespace art
169
170 #endif  // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_FS_H_