From a491594775cb32773798a4acb5e278e0765a498e Mon Sep 17 00:00:00 2001 From: chrox Date: Fri, 3 Oct 2014 16:07:38 +0800 Subject: [PATCH 1/3] trace package loading process in koreader --- utils/trace_require.lua | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 utils/trace_require.lua diff --git a/utils/trace_require.lua b/utils/trace_require.lua new file mode 100644 index 000000000..a5e00668d --- /dev/null +++ b/utils/trace_require.lua @@ -0,0 +1,47 @@ +-- trace package loading flow with require call +-- usage: ./luajit -lutils/trace_require reader.lua ../../test + +local math = require("math") +local _require = require + +local loaded_modules = {} +local highlight = "*" +-- timing threshold for highlight annotation +local threshold = 0.001 +-- whether to trace loaded packages +local trace_loaded = false +local indent = string.rep(" ", 4) + +local level = 0 +function require(module) + level = level + 1 + local x = os.clock() + local info = debug.getinfo(2) + -- this is a protected call of require + if info.short_src == "[C]" then + info = debug.getinfo(3) + end + local loaded = loaded_modules[module] + if not loaded then + print(string.format("%s%s:%s => %s", + indent:rep(level), info.short_src, info.currentline, module)) + elseif trace_loaded then + print(string.format("%s%s:%s -> %s", + indent:rep(level), info.short_src, info.currentline, module)) + end + -- protect require call in case we cannot raise call level when errors happen + local ok, loaded_module = pcall(_require, module) + if not ok then + level = level - 1 + error(loaded_module) + end + local elapse = os.clock() - x + local annot = highlight:rep(math.ceil(math.log10(elapse/threshold))) or "" + if not loaded then + print(string.format("%s%s loading time: %.3f", + annot .. indent:rep(level):sub(#annot + 1), module, elapse)) + end + loaded_modules[module] = true + level = level - 1 + return loaded_module +end From 14e2bf3d69e3375beb991b5afe19dd798cd76b8c Mon Sep 17 00:00:00 2001 From: chrox Date: Fri, 3 Oct 2014 16:11:53 +0800 Subject: [PATCH 2/3] refatoring: lazy loading of packages --- .../apps/filemanager/filemanagerhistory.lua | 2 +- frontend/apps/filemanager/filemanagersearch.lua | 3 ++- frontend/ui/device.lua | 17 +++++++++++++---- frontend/ui/device/kobopowerd.lua | 3 ++- reader.lua | 3 ++- wtest.lua => utils/wtest.lua | 3 ++- 6 files changed, 22 insertions(+), 9 deletions(-) rename wtest.lua => utils/wtest.lua (99%) diff --git a/frontend/apps/filemanager/filemanagerhistory.lua b/frontend/apps/filemanager/filemanagerhistory.lua index a79999f66..cd266a46b 100644 --- a/frontend/apps/filemanager/filemanagerhistory.lua +++ b/frontend/apps/filemanager/filemanagerhistory.lua @@ -1,7 +1,6 @@ local InputContainer = require("ui/widget/container/inputcontainer") local CenterContainer = require("ui/widget/container/centercontainer") local ButtonDialog = require("ui/widget/buttondialog") -local ReaderUI = require("apps/reader/readerui") local lfs = require("libs/libkoreader-lfs") local UIManager = require("ui/uimanager") local DocSettings = require("docsettings") @@ -79,6 +78,7 @@ function FileManagerHistory:addToMainMenu(tab_item_table) end function FileManagerHistory:updateItemTable() + local ReaderUI = require("apps/reader/readerui") self.hist = {} for f in lfs.dir(history_dir) do diff --git a/frontend/apps/filemanager/filemanagersearch.lua b/frontend/apps/filemanager/filemanagersearch.lua index 773077ab8..d4add2dc1 100644 --- a/frontend/apps/filemanager/filemanagersearch.lua +++ b/frontend/apps/filemanager/filemanagersearch.lua @@ -3,7 +3,6 @@ local InputContainer = require("ui/widget/container/inputcontainer") local DocumentRegistry = require("document/documentregistry") local InputDialog = require("ui/widget/inputdialog") local InfoMessage = require("ui/widget/infomessage") -local ReaderUI = require("apps/reader/readerui") local lfs = require("libs/libkoreader-lfs") local UIManager = require("ui/uimanager") local Menu = require("ui/widget/menu") @@ -498,6 +497,7 @@ function Search:onMenuHold(item) end function Search:showresults() + local ReaderUI = require("apps/reader/readerui") local menu_container = CenterContainer:new{ dimen = Screen:getSize(), } @@ -544,6 +544,7 @@ function Search:showresults() end function Search:browse(option,run,chosen) + local ReaderUI = require("apps/reader/readerui") local restart_me = false local menu_container = CenterContainer:new{ dimen = Screen:getSize(), diff --git a/frontend/ui/device.lua b/frontend/ui/device.lua index 4e36f8d5d..cba781791 100644 --- a/frontend/ui/device.lua +++ b/frontend/ui/device.lua @@ -1,8 +1,4 @@ -local AndroidPowerd = require("ui/device/androidpowerd") -local KindlePowerD = require("ui/device/kindlepowerd") local isAndroid, android = pcall(require, "android") -local KoboPowerD = require("ui/device/kobopowerd") -local BasePowerD = require("ui/device/basepowerd") local lfs = require("libs/libkoreader-lfs") local Screen = require("ui/device/screen") local util = require("ffi/util") @@ -288,12 +284,16 @@ function Device:getPowerDevice() else local model = self:getModel() if model == "KindleTouch" or model == "KindlePaperWhite" or model == "KindlePaperWhite2" then + local KindlePowerD = require("ui/device/kindlepowerd") self.powerd = KindlePowerD:new{model = model} elseif self:isKobo() then + local KoboPowerD = require("ui/device/kobopowerd") self.powerd = KoboPowerD:new() elseif self.isAndroid then + local AndroidPowerd = require("ui/device/androidpowerd") self.powerd = AndroidPowerd:new() else -- emulated FrontLight + local BasePowerD = require("ui/device/basepowerd") self.powerd = BasePowerD:new() end end @@ -322,4 +322,13 @@ function Device:isSpecialOffers() return self.is_special_offers end +-- FIXME: this is a dirty hack, normally we don't need to get power device this early, +-- but Kobo devices somehow may need to init the frontlight module at startup? +-- because `kobolight = require("ffi/kobolight")` used to be in the `koreader-base` script +-- and run as the first line of koreader script no matter which device you are running on, +-- which is utterly ugly. So I refactored it into the `init` method of `kobopowerd` and +-- `kobolight` will be init here. It's pretty safe to comment this line for non-kobo devices +-- so if kobo users find this line is useless, please don't hesitate to get rid of it. +local dummy_powerd = Device:getPowerDevice() + return Device diff --git a/frontend/ui/device/kobopowerd.lua b/frontend/ui/device/kobopowerd.lua index 0816276d0..103ed5446 100644 --- a/frontend/ui/device/kobopowerd.lua +++ b/frontend/ui/device/kobopowerd.lua @@ -5,7 +5,7 @@ local KoboPowerD = BasePowerD:new{ flIntensity = 20, restore_settings = true, fl = nil, - + batt_capacity_file = "/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/capacity", is_charging_file = "/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/charge_now", battCapacity = nil, @@ -13,6 +13,7 @@ local KoboPowerD = BasePowerD:new{ } function KoboPowerD:init() + local kobolight = require("ffi/kobolight") local ok, light = pcall(kobolight.open) if ok then self.fl = light end end diff --git a/reader.lua b/reader.lua index ef59703fd..f463fcba1 100755 --- a/reader.lua +++ b/reader.lua @@ -1,4 +1,4 @@ -#!./koreader-base +#!./luajit require "defaults" pcall(dofile, "defaults.persistent.lua") @@ -100,6 +100,7 @@ local function showusage() return end +local ARGV = arg local argidx = 1 while argidx <= #ARGV do local arg = ARGV[argidx] diff --git a/wtest.lua b/utils/wtest.lua similarity index 99% rename from wtest.lua rename to utils/wtest.lua index 24ec22c3a..e9112f7fd 100755 --- a/wtest.lua +++ b/utils/wtest.lua @@ -1,4 +1,5 @@ -#!./koreader-base +-- widget test utility +-- usage: ./luajit util/wtest.lua require "defaults" print(package.path) From 2d3c502948645d3d76b7d58e0a399b33fad99b89 Mon Sep 17 00:00:00 2001 From: chrox Date: Fri, 3 Oct 2014 18:02:50 +0800 Subject: [PATCH 3/3] move platform dependent files into 'platform' directory --- .gitmodules | 4 +-- Makefile | 29 ++++++++++-------- {android => platform/android}/llapp_main.lua | 0 {android => platform/android}/luajit-launcher | 0 .../kindle}/extensions/koreader/README.txt | 0 .../extensions/koreader/bin/koreader-ext.sh | 0 .../kindle}/extensions/koreader/config.xml | 0 .../kindle}/extensions/koreader/menu.json | 0 {kindle => platform/kindle}/koreader.sh | 0 {kindle => platform/kindle}/kotar_cpoint | 0 .../kindle}/launchpad/koreader.ini | 0 {kindle => platform/kindle}/libkohelper.sh | 0 {kobo => platform/kobo}/fmon/README.txt | 0 {kobo => platform/kobo}/fmon/koreader.sh | 0 {kobo => platform/kobo}/koreader.sh | 0 {kobo => platform/kobo}/nickel.sh | 0 {kobo => platform/kobo}/suspend.sh | 0 {windows => platform/win32}/SDL2.dll | Bin .../win32}/libgcc_s_sjlj-1.dll | Bin {windows => platform/win32}/libstdc++-6.dll | Bin .../win32}/libwinpthread-1.dll | Bin 21 files changed, 19 insertions(+), 14 deletions(-) rename {android => platform/android}/llapp_main.lua (100%) rename {android => platform/android}/luajit-launcher (100%) rename {kindle => platform/kindle}/extensions/koreader/README.txt (100%) rename {kindle => platform/kindle}/extensions/koreader/bin/koreader-ext.sh (100%) rename {kindle => platform/kindle}/extensions/koreader/config.xml (100%) rename {kindle => platform/kindle}/extensions/koreader/menu.json (100%) rename {kindle => platform/kindle}/koreader.sh (100%) rename {kindle => platform/kindle}/kotar_cpoint (100%) rename {kindle => platform/kindle}/launchpad/koreader.ini (100%) rename {kindle => platform/kindle}/libkohelper.sh (100%) rename {kobo => platform/kobo}/fmon/README.txt (100%) rename {kobo => platform/kobo}/fmon/koreader.sh (100%) rename {kobo => platform/kobo}/koreader.sh (100%) rename {kobo => platform/kobo}/nickel.sh (100%) rename {kobo => platform/kobo}/suspend.sh (100%) rename {windows => platform/win32}/SDL2.dll (100%) rename {windows => platform/win32}/libgcc_s_sjlj-1.dll (100%) rename {windows => platform/win32}/libstdc++-6.dll (100%) rename {windows => platform/win32}/libwinpthread-1.dll (100%) diff --git a/.gitmodules b/.gitmodules index 4000f8f41..fb096c5c1 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "koreader-base"] path = koreader-base url = git://github.com/koreader/koreader-base.git -[submodule "android/luajit-launcher"] - path = android/luajit-launcher +[submodule "platform/android/luajit-launcher"] + path = platform/android/luajit-launcher url = https://github.com/koreader/android-luajit-launcher.git diff --git a/Makefile b/Makefile index f82da93e9..16285ce6b 100644 --- a/Makefile +++ b/Makefile @@ -14,8 +14,13 @@ export PATH:=$(CURDIR)/$(KOR_BASE)/toolchain/android-toolchain/bin:$(PATH) MACHINE?=$(shell PATH=$(PATH) $(CC) -dumpmachine 2>/dev/null) INSTALL_DIR=koreader-$(MACHINE) -ANDROID_DIR=android +# platform directories +PLATFORM_DIR=platform +KINDLE_DIR=$(PLATFORM_DIR)/kindle +KOBO_DIR=$(PLATFORM_DIR)/kobo +ANDROID_DIR=$(PLATFORM_DIR)/android ANDROID_LAUNCHER_DIR:=$(ANDROID_DIR)/luajit-launcher +WIN32_DIR=$(PLATFORM_DIR)/win32 # files to link from main directory INSTALL_FILES=reader.lua frontend resources defaults.lua l10n \ @@ -54,7 +59,7 @@ ifdef ANDROID endif ifdef WIN32 # install runtime libraries for win32 - cd $(INSTALL_DIR)/koreader && cp ../../windows/*.dll . + cd $(INSTALL_DIR)/koreader && cp ../../$(WIN32_DIR)/*.dll . endif # install plugins cp -r plugins/* $(INSTALL_DIR)/koreader/plugins/ @@ -102,11 +107,11 @@ kindleupdate: all # remove old package if any rm -f koreader-kindle-$(MACHINE)-$(VERSION).zip # Kindle launching scripts - ln -sf ../kindle/extensions $(INSTALL_DIR)/ - ln -sf ../kindle/launchpad $(INSTALL_DIR)/ - ln -sf ../../kindle/koreader.sh $(INSTALL_DIR)/koreader - ln -sf ../../kindle/libkohelper.sh $(INSTALL_DIR)/koreader - ln -sf ../../kindle/kotar_cpoint $(INSTALL_DIR)/koreader + ln -sf ../$(KINDLE_DIR)/extensions $(INSTALL_DIR)/ + ln -sf ../$(KINDLE_DIR)/launchpad $(INSTALL_DIR)/ + ln -sf ../../$(KINDLE_DIR)/koreader.sh $(INSTALL_DIR)/koreader + ln -sf ../../$(KINDLE_DIR)/libkohelper.sh $(INSTALL_DIR)/koreader + ln -sf ../../$(KINDLE_DIR)/kotar_cpoint $(INSTALL_DIR)/koreader # create new package # Don't bundle launchpad on touch devices.. ifeq ($(TARGET), kindle-legacy) @@ -137,13 +142,13 @@ koboupdate: all rm -f koreader-kobo-$(MACHINE)-$(VERSION).zip # Kobo launching scripts mkdir -p $(INSTALL_DIR)/kobo/mnt/onboard/.kobo - ln -sf ../../../../../kobo/fmon $(INSTALL_DIR)/kobo/mnt/onboard/.kobo/ + ln -sf ../../../../../$(KOBO_DIR)/fmon $(INSTALL_DIR)/kobo/mnt/onboard/.kobo/ cd $(INSTALL_DIR)/kobo && tar -czhf ../KoboRoot.tgz mnt cp resources/koreader.png $(INSTALL_DIR)/koreader.png - cp kobo/fmon/README.txt $(INSTALL_DIR)/README_kobo.txt - cp kobo/koreader.sh $(INSTALL_DIR)/koreader - cp kobo/suspend.sh $(INSTALL_DIR)/koreader - cp kobo/nickel.sh $(INSTALL_DIR)/koreader + cp $(KOBO_DIR)/fmon/README.txt $(INSTALL_DIR)/README_kobo.txt + cp $(KOBO_DIR)/koreader.sh $(INSTALL_DIR)/koreader + cp $(KOBO_DIR)/suspend.sh $(INSTALL_DIR)/koreader + cp $(KOBO_DIR)/nickel.sh $(INSTALL_DIR)/koreader # create new package cd $(INSTALL_DIR) && \ zip -9 -r \ diff --git a/android/llapp_main.lua b/platform/android/llapp_main.lua similarity index 100% rename from android/llapp_main.lua rename to platform/android/llapp_main.lua diff --git a/android/luajit-launcher b/platform/android/luajit-launcher similarity index 100% rename from android/luajit-launcher rename to platform/android/luajit-launcher diff --git a/kindle/extensions/koreader/README.txt b/platform/kindle/extensions/koreader/README.txt similarity index 100% rename from kindle/extensions/koreader/README.txt rename to platform/kindle/extensions/koreader/README.txt diff --git a/kindle/extensions/koreader/bin/koreader-ext.sh b/platform/kindle/extensions/koreader/bin/koreader-ext.sh similarity index 100% rename from kindle/extensions/koreader/bin/koreader-ext.sh rename to platform/kindle/extensions/koreader/bin/koreader-ext.sh diff --git a/kindle/extensions/koreader/config.xml b/platform/kindle/extensions/koreader/config.xml similarity index 100% rename from kindle/extensions/koreader/config.xml rename to platform/kindle/extensions/koreader/config.xml diff --git a/kindle/extensions/koreader/menu.json b/platform/kindle/extensions/koreader/menu.json similarity index 100% rename from kindle/extensions/koreader/menu.json rename to platform/kindle/extensions/koreader/menu.json diff --git a/kindle/koreader.sh b/platform/kindle/koreader.sh similarity index 100% rename from kindle/koreader.sh rename to platform/kindle/koreader.sh diff --git a/kindle/kotar_cpoint b/platform/kindle/kotar_cpoint similarity index 100% rename from kindle/kotar_cpoint rename to platform/kindle/kotar_cpoint diff --git a/kindle/launchpad/koreader.ini b/platform/kindle/launchpad/koreader.ini similarity index 100% rename from kindle/launchpad/koreader.ini rename to platform/kindle/launchpad/koreader.ini diff --git a/kindle/libkohelper.sh b/platform/kindle/libkohelper.sh similarity index 100% rename from kindle/libkohelper.sh rename to platform/kindle/libkohelper.sh diff --git a/kobo/fmon/README.txt b/platform/kobo/fmon/README.txt similarity index 100% rename from kobo/fmon/README.txt rename to platform/kobo/fmon/README.txt diff --git a/kobo/fmon/koreader.sh b/platform/kobo/fmon/koreader.sh similarity index 100% rename from kobo/fmon/koreader.sh rename to platform/kobo/fmon/koreader.sh diff --git a/kobo/koreader.sh b/platform/kobo/koreader.sh similarity index 100% rename from kobo/koreader.sh rename to platform/kobo/koreader.sh diff --git a/kobo/nickel.sh b/platform/kobo/nickel.sh similarity index 100% rename from kobo/nickel.sh rename to platform/kobo/nickel.sh diff --git a/kobo/suspend.sh b/platform/kobo/suspend.sh similarity index 100% rename from kobo/suspend.sh rename to platform/kobo/suspend.sh diff --git a/windows/SDL2.dll b/platform/win32/SDL2.dll similarity index 100% rename from windows/SDL2.dll rename to platform/win32/SDL2.dll diff --git a/windows/libgcc_s_sjlj-1.dll b/platform/win32/libgcc_s_sjlj-1.dll similarity index 100% rename from windows/libgcc_s_sjlj-1.dll rename to platform/win32/libgcc_s_sjlj-1.dll diff --git a/windows/libstdc++-6.dll b/platform/win32/libstdc++-6.dll similarity index 100% rename from windows/libstdc++-6.dll rename to platform/win32/libstdc++-6.dll diff --git a/windows/libwinpthread-1.dll b/platform/win32/libwinpthread-1.dll similarity index 100% rename from windows/libwinpthread-1.dll rename to platform/win32/libwinpthread-1.dll