Skip to content

Conversation

PeterDaveHello
Copy link
Collaborator

Should build parallelly to make the whole process faster.

@ljharb
Copy link
Member

ljharb commented May 23, 2015

Can you explain a bit more about what's happening in this PR?

@@ -1043,6 +1043,9 @@ nvm_install_node_source() {
if [ "_$NVM_OS" = "_freebsd" ]; then
make='gmake'
MAKE_CXX="CXX=c++"
MAKE_JOBS="`sysctl hw.ncpu | awk '{print $2}'`"
Copy link
Member

Choose a reason for hiding this comment

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

please use $() instead of backticks; same below

@PeterDaveHello
Copy link
Collaborator Author

man make

   -j [jobs], --jobs[=jobs]
        Specifies the number of jobs (commands) to run simultaneously.  If there is more than one -j option, the last one is effective.  If the -j option is given without  an  argu‐
        ment, make will not limit the number of jobs that can run simultaneously.

If you have multi-core/processor platform, should let them work in parallel to speed up the build, there is no reason to use only one thread to build on multi-core/processor computer.

@ljharb
Copy link
Member

ljharb commented May 23, 2015

Thank you, but i mean, what is nproc and sysctl, and what does MAKE_JOBS actually represent?

What happens now that this is set? Is there any reason a user might not want this setting turned on?

@PeterDaveHello
Copy link
Collaborator Author

@ljharb It's use to determinate how many thread(s) should make use.

@ljharb
Copy link
Member

ljharb commented May 23, 2015

So is nproc the number of processors the machine has? Wouldn't it be better not to use 100% of available CPUs, but instead N - 1?

@PeterDaveHello
Copy link
Collaborator Author

@ljharb
Copy link
Member

ljharb commented May 23, 2015

On my Mac, I get -bash: nproc: command not found.

Any commands we use must be POSIX-compliant.

@PeterDaveHello
Copy link
Collaborator Author

Should work on mac now.

elif [ "_$NVM_OS" = "_sunos" ]; then
MAKE_JOBS="$(psrinfo | wc -l)"
else
MAKE_JOBS="1"
Copy link
Member

Choose a reason for hiding this comment

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

Is there a chance that any of the other commands could fail, making MAKE_JOBS be the empty string, and not hitting this else block?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It will be very special case I think, but if you want, I can modify it to:

  if [ "_$NVM_OS" = "_linux" ]; then
    MAKE_JOBS="$(nproc)"
  elif [ "_$NVM_OS" = "_freebsd" ] || [ "_$NVM_OS" = "_darwin" ]; then
    MAKE_JOBS="$(sysctl -n hw.ncpu)"
  elif [ "_$NVM_OS" = "_sunos" ]; then
    MAKE_JOBS="$(psrinfo | wc -l)"
  fi

  if [ -z "$MAKE_JOBS" ]; then
    MAKE_JOBS="1"
  fi

Copy link
Member

Choose a reason for hiding this comment

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

I agree, but I think that'd be more reliable. Thanks for being diligent!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I would like to add additional info in that part, hope you don't mind.

@ljharb
Copy link
Member

ljharb commented May 24, 2015

Looks like raspbian, ie a raspberry pi, might not have any of these three commands - also centOS 5.5 and Ubuntu 9.10 might not. However, grep -c 'core id' /proc/cpuinfo should work on any system where [ -f /proc/cpuinfo ]

@PeterDaveHello
Copy link
Collaborator Author

I think we can assume linux will have /proc/cpuinfo

@Efreak
Copy link

Efreak commented Jun 7, 2015

grep -c 'core id' /proc/cpuinfo should work on any system where [ -f /proc/cpuinfo ]

This is incorrect. I'm running a quad-core odroid-c1 with trusty on it, and /proc/cpuinfo doesn't contain 'core id'.

There's probably a few dozen ways of obtaining this information, however the most portable solution of all is likely to be getconf _NPROCESSORS_ONLN; getconf is part of the POSIX specification, and _NPROCESSORS_ONLN isn't mandatory, but is supported by both OS X and linux. Even cygwin supports it (though nodejs still doesn't support cygwin 😦)

The best way, however, might also be to just allow passing of custom parameters to the build system.

@ljharb
Copy link
Member

ljharb commented Jun 7, 2015

Sounds like getconf _NPROCESSORS_ONLN plus an installation parameter to override the processor number may be the simplest/best approach here?

@PeterDaveHello
Copy link
Collaborator Author

hmmmmm ... anyway, we can fallback to 1 if not detect, and there is also warning msg, why not just enable this feature first and improve it in future? Current code should work on most of the systems.

@ljharb
Copy link
Member

ljharb commented Jun 8, 2015

The concern would be that building on more processors without informing the user might be taking more processor resources than the user actually wants it to take.

@PeterDaveHello
Copy link
Collaborator Author

What about adding an option for non-multithreading compiling?

@ljharb
Copy link
Member

ljharb commented Sep 27, 2015

@PeterDaveHello That could be fine. I'm concerned about the default, though. Perhaps it's OK though if the default is N - 1, and it can be overridden to any number the user wants?

@PeterDaveHello
Copy link
Collaborator Author

Agree!

@ljharb ljharb added installing node Issues with installing node/io.js versions. feature requests I want a new feature in nvm! needs rebase Please rebase your branch onto latest master! This removes merge commits, & keeps the git log clean. labels Sep 27, 2015
@PeterDaveHello
Copy link
Collaborator Author

@ljharb I just updated this PR, now it support -j paramater followed -s to specified the CPU threads we can use, can you give me advice about the document?

@@ -1231,6 +1231,25 @@ nvm_install_node_source() {
make='gmake'
MAKE_CXX="CXX=c++"
fi

if [ -z "$NVM_CPU_THREADS" ] || [[ ! $NVM_CPU_THREADS =~ ^[0-9]+$ ]] ; then
Copy link
Member

Choose a reason for hiding this comment

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

this syntax is bash-specific - nvm must work on all POSIX systems, including ksh, dash, sh, bash, and zsh.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh okay, I'll modify it.

@PeterDaveHello
Copy link
Collaborator Author

@ljharb I'm not sure why this test failed: https://travis-ci.org/creationix/nvm/jobs/98080035, do you mind to point me out? Thanks a lot!

@PeterDaveHello
Copy link
Collaborator Author

Failed on the new installation test of sh/dash but didn't see any useful info in the build log...

@ljharb
Copy link
Member

ljharb commented Dec 22, 2015

You can definitely ignore the nvm_reset install_script failure for now. The others seem legitimate.

@PeterDaveHello
Copy link
Collaborator Author

Yeah, and I can't pass the test I wrote now 😈 trying to fix it

@PeterDaveHello PeterDaveHello changed the title Enable multiple jobs for when build from source Enable multiple jobs for make when build from source Dec 23, 2015
@PeterDaveHello
Copy link
Collaborator Author

I just dropped a robustness test of giving non-expected value for -j, not so required.

@PeterDaveHello
Copy link
Collaborator Author

@ljharb is it good to merge now?

echo "Please report an issue on GitHub to help us make it better and run it faster on your computer!" 1>&2
NVM_MAKE_JOBS="1"
else
NVM_MAKE_JOBS=$(($NVM_CPU_THREADS - 1))
Copy link
Member

Choose a reason for hiding this comment

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

can we output something to stdout here indicating what the number of threads has been automatically set to?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Of course.

@ljharb
Copy link
Member

ljharb commented Dec 25, 2015

When I tried nvm install -j foo -s 0.12.2 I got grep: invalid option -- j although nvm install -s -j foo 0.12.2 worked as expected.

@PeterDaveHello
Copy link
Collaborator Author

When I tried nvm install -j foo -s 0.12.2 I got grep: invalid option -- j although nvm install -s -j foo 0.12.2 worked as expected.

Yep I didn't handle out of order input yet, any suggestion on this?

@PeterDaveHello
Copy link
Collaborator Author

Updated for output first.

@ljharb
Copy link
Member

ljharb commented Dec 26, 2015

For the argument ordering: I think you'll want to loop through the arguments, stopping when you get to the first one that's not -s or -j - and when you find one of those, set the appropriate vars (and for the case of -j, use shift to grab the next item, naturally). That should be easy to extend in the future if we add more nvm install arguments.

@PeterDaveHello
Copy link
Collaborator Author

Can we implement that as another feature? Maybe this PR is just for essential support for multiple threads.

@ljharb
Copy link
Member

ljharb commented Dec 27, 2015

@PeterDaveHello sure, can you rebase once more? that install_script test won't fail anymore

@PeterDaveHello
Copy link
Collaborator Author

Okay, rebased. Do you know the problem that cause build failed before?

@ljharb
Copy link
Member

ljharb commented Dec 27, 2015

@PeterDaveHello yes, travis-ci/travis-ci#5363 is the culprit

@PeterDaveHello
Copy link
Collaborator Author

👍

@PeterDaveHello
Copy link
Collaborator Author

Test passed 😄

ljharb added a commit that referenced this pull request Dec 27, 2015
[New] `nvm install`: Enable multiple jobs for make when build from source via `-j`
@ljharb ljharb merged commit d40eca8 into nvm-sh:master Dec 27, 2015
NVM_CPU_THREAD_VALID=$(nvm_is_natural_num $1)
if [ "$NVM_CPU_THREAD_VALID" = "true" ]; then
NVM_MAKE_JOBS=$1
echo "Set number of jobs to $MAKE_JOBS for 'make' utility"
Copy link
Member

Choose a reason for hiding this comment

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

this var name is incorrect (i'll fix in a followup)

@ljharb
Copy link
Member

ljharb commented Dec 27, 2015

Followup: f279837

@PeterDaveHello PeterDaveHello deleted the parallel_build branch December 28, 2015 05:52
@PeterDaveHello
Copy link
Collaborator Author

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature requests I want a new feature in nvm! installing node Issues with installing node/io.js versions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants