main函数在: /src/mongo/db/db.cpp
主函数实现的步骤在: main -> mongoDbMain
主要工作:
- 参数解析
- 内存文件flush到硬盘 (线程)
- 服务监听
详细事情:
- 检测CPU是大端还是小端,大端就不支持了
- 解析输入的命令参数
- 每隔一分钟对所有内存映射文件进行flush操作到磁盘dataFileSync.go();
- 服务监听 initAndListen
1234 static int mongoDbMain(int argc, char* argv[], char **envp) {
1235 static StaticObserver staticObserver;
1236
1237 getcurns = ourgetns;
1238
1239 setupSignalHandlers();
1240
1241 dbExecCommand = argv[0]; // 此处莫非是暗示我们所有的binary都是由这一个main所在文件build出来的?
1242
1243 srand(curTimeMicros()); // 设置随机数种子
1244
1245 {
1246 unsigned x = 0x12345678;
1247 unsigned char& b = (unsigned char&) x;
1248 if ( b != 0x78 ) {
1249 out() << "big endian cpus not yet supported" << endl;
1250 return 33;
1251 }
1252 }
1253
1254 if( argc == 1 )
1255 cout << dbExecCommand << " --help for help and startup options" << endl;
1256
1257 // 输入参数解析
1258 processCommandLineOptions(std::vector<std::string>(argv, argv + argc));
1259 mongo::runGlobalInitializersOrDie(argc, argv, envp);
1260 CmdLine::censor(argc, argv);
1261
1262 if (!initializeServerGlobalState())
1263 ::_exit(EXIT_FAILURE);
1264
1265 // Per SERVER-7434, startInterruptThread() must run after any forks
1266 // (initializeServerGlobalState()) and before creation of any other threads.
1267 startInterruptThread();
1268
1269 dataFileSync.go(); // 将内存数据flush到磁盘
1270
1271 #if defined(_WIN32)
1272 if (ntservice::shouldStartService()) {
1273 ntservice::startService();
1274 // exits directly and so never reaches here either.
1275 }
1276 #endif
1277
1278 StartupTest::runTests();
1279 initAndListen(cmdLine.port); // 服务监听
1280 dbexit(EXIT_CLEAN);
1281 return 0;
1282 }