Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP : Add KIT in process mode #7329

Conversation

Darshan-upadhyay1110
Copy link
Contributor

Signed-off-by: Darshan-upadhyay1110 [email protected]

Change-Id: I153bdeed50e86c9a97a60591f2644eb6b4c51f00

  • have a mode KITINPROCESS (limited to --enable-debug) where everything is running as a single process
  • global function that will know if we're in kit-in-process mode,=> Util::isKitInProcess()

@Darshan-upadhyay1110 Darshan-upadhyay1110 force-pushed the private/darshan/Kit-in-process branch 2 times, most recently from d0cefb5 to 54ad034 Compare September 29, 2023 14:50
{
LOG_ERR("Conversion from " << extension << " to " << newExtension << " failed (" << rc << ").");
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Command used to be a separate process. It has a main function. Probably you need an else branch here, so that you invoke that main function on a thread?

configure.ac Outdated
@@ -192,6 +192,10 @@ AC_ARG_ENABLE([androidapp],
to work similarly to the iOS app, from the JavaScript and the pseudo WebSocket
message plumbing point of view.]))

AC_ARG_ENABLE([kitinprocess],
AS_HELP_STRING([--enable-kitinprocess],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: probably this is not needed, this mode could be enabled at runtime with a cmdline switch and just be enabled in debug mode. The more ifdefs we have, the more likely CI doesn't catch a build breakage.

@Darshan-upadhyay1110 Darshan-upadhyay1110 force-pushed the private/darshan/Kit-in-process branch 8 times, most recently from ad8bec1 to 9817c36 Compare October 3, 2023 12:10
 - have a mode KITINPROCESS (limited to --enable-debug) where everything is running as a single process
 - global function that will know if we're in kit-in-process mode,=> Util::isKitInProcess()
Signed-off-by: Darshan-upadhyay1110 <[email protected]>

Change-Id: I153bdeed50e86c9a97a60591f2644eb6b4c51f00
Signed-off-by: Darshan-upadhyay1110 <[email protected]>
Copy link
Contributor

@mmeeks mmeeks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a fine start - I expect we'll want a different coolwsd process - this is most likely the easiest way to get everything linked into one binary.

Create a cool-onedoc binary or something in the Makefile.am - that links in all of the coolwsd and coolforkit source files - and then tweak / unwind the conflicts / overlaps etc.

Remove the 'spawnProcess' that starts the forkit - and instead run its 'main' or some other entry point you can add.

To avoid both 'main' functions conflicting - it's likely we'll need to split out two very small 'coolwsd-main' and 'forkit-main' files that just call an extern "C" int coolwsd_main(int argv, char **argv); type function each - since we can't have two main functions in the same linked object.

Does that help ?

Makefile.am Outdated
@@ -510,6 +510,12 @@ run-trace: setup-wsd
--o:trace[@enable]=true --o:trace.path=${builddir}/trace.txt.gz \
--o:trace.outgoing.record=false

run-valgrind-kitinprocess: setup-wsd
@echo "Launching coolwsd under valgrind (but not forkit/coolkit, yet)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a promising start. The idea of an unmodified coolwsd, and (perhaps) building a separate forkit which is a dynamic library we can load into the main process (or something) is possibly a good one. We want to have the forkit running in the same process as coolwsd I think.

kit/ForKit.cpp Outdated
@@ -383,50 +383,61 @@ static int createLibreOfficeKit(const std::string& childRoot,
LOG_DBG("Forking a coolkit process with jailId: " << jailId << " as spare coolkit #"
<< spareKitId << '.');
const auto startForkingTime = std::chrono::steady_clock::now();
pid_t pid = 0;
if (Util::isKitInProcess()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense of course - we should of course guard against doing this twice - having two threads in lokit_main will be a nightmare =) can you add a LOG_ERR and perhaps a sleeping loop for that mode - I guess we have this pre-forking logic that will try to always launch a 2nd idle process when one is used so - sleeping rather than asserting makes sense for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question : i am not sure about which mode you are refering here. is it the KitInProcess where the new thread has been created and we call lokit_main ?

and the LOG_ERR is also for that main function call right ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOK is effectively single threaded - it uses plenty of global state. You can't run two threads running lokit_main in the same process and expect it to work flawlessly. So - we should warn / block / stop this in the case that two of these are requested.

@Darshan-upadhyay1110 Darshan-upadhyay1110 force-pushed the private/darshan/Kit-in-process branch 10 times, most recently from 483dc72 to 7c63f31 Compare October 10, 2023 16:04
wsd/COOLWSD.cpp Outdated
if (requestURI.getPath() == FORKIT_URI)
{
if (socket->getPid() != COOLWSD::ForKitProcId)
if(!Util::isKitInProcess()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use clang-format to have uniform formatting everywhere. You can configure vscode to apply formatting only on edited lines.

Copy link
Contributor

@Ashod Ashod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Darshan-upadhyay1110. I left some comments and suggestions, as well as questions.

common/Util.cpp Outdated
Comment on lines 331 to 332
const char* kitInProcess = std::getenv("KIT_IN_PROCESS");
return (kitInProcess != nullptr && strcmp(kitInProcess, "true") == 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be static. Also, is there a reason to have it set to "true" and "false"? We can simply define the KIT_IN_PROCESS for 'true' and not define it for 'false'. And avoid this comparison (just check for != nullptr).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Darshan-upadhyay1110 as you have separate binaries now: regular one and "single process" one, that env variable is not needed I think.
You should make this true when you link single process code probably.

kit/ForKit.cpp Outdated
Comment on lines 393 to 399
}
else {
pid = fork();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run clang-format, or apply proper formatting. This is all wrong.

Copy link
Contributor Author

@Darshan-upadhyay1110 Darshan-upadhyay1110 Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @Ashod i updated the formatting mistakes but there might be still some present sorry for that. i am still working on this patch so will update all formatting mistakes soon...

kit/ForKit.cpp Outdated
Comment on lines 386 to 391
std::thread ([childRoot, jailId, sysTemplate, loTemplate, queryVersion]
{
Util::setThreadName("kit_in_process");
lokit_main(childRoot, jailId, sysTemplate, loTemplate, NoCapsForKit, NoSeccomp,
queryVersion, DisplayVersion, spareKitId);
}).detach();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we detaching? Why isn't the thread owned by coolwsd?

Can you share the design of this whole PR please?

kit/ForKit.cpp Outdated
#ifndef KIT_IN_PROCESS
UnitKit::get().launchedKit(pid);
#endif
if (!Util::isKitInProcess()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use javascript formatting in C++ code. Apply clang-format if it's too much work.

Comment on lines 44 to 46
// Wait for the thread to finish and get the result using a future.
std::future<int> resultFuture = resultPromise.get_future();
int result = resultFuture.get();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proper way of waiting for a thread to finish is to join the thread.

There is no good reason to detach the thread and use a promise/future.

int result = 0;
std::thread worker = std::thread ([&] {
        Util::setThreadName("debug main kit_in_process");
        result = forkit_main(args.size(), argv);
    });

worker.join();
return result;

@@ -0,0 +1,40 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not clear to me why we need all these new files. Is there an explanation on the design somewhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ashod comment above will explain: #7329 (review)

@@ -366,6 +361,8 @@ static void cleanupChildren()
}
}

static bool lokitProcessIsRunning = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments so it is clear what the various variables/functions do. It's not clear what this is supposed to represent and how it's supposed to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have added in some places but as i may change code bits as per task progress further so probably add all commets once i got working build , thanks :)

wsd/COOLWSD.cpp Outdated
Comment on lines 6359 to 6361
if (!Util::isKitInProcess()) {
SigUtil::setFatalSignals("wsd " COOLWSD_VERSION " " COOLWSD_VERSION_HASH);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Why remove the fatal signal in 'kit in process' mode?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh yes, sorry we only need to remove fatal signal for Forkit and Kit not for COOLWSD ( pushing that change...)

- debug bin program for single Kit in process
- split out two very small 'coolwsd-main' and 'forkit-main'
- add debug-main for kit in process
Signed-off-by: Darshan-upadhyay1110 <[email protected]>
Change-Id: Iff8d829feead221b1f08c53fb1dbfb4241757601
@Darshan-upadhyay1110 Darshan-upadhyay1110 force-pushed the private/darshan/Kit-in-process branch from 7c63f31 to 1116bb9 Compare October 12, 2023 16:50
Signed-off-by: Darshan-upadhyay1110 <[email protected]>
Change-Id: I6bf029df1f9b7fe395ecfdf6144a1869efcf0833
Signed-off-by: Jaume Pujantell <[email protected]>
Change-Id: Ib08067e83ab08ce5bc525f069357218b3d3614db
@JaumePujantell
Copy link

Push my current local state (not working) so @caolanm can see it.

@mmeeks
Copy link
Contributor

mmeeks commented Feb 27, 2024

Eventually we merged this in another pull request; thanks Darshan =)

@mmeeks mmeeks closed this Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

7 participants