Is it possible to control cpu usage?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Is it possible to control cpu usage?

Post by agriz »

I checked the cpu usage. It is reaches 100% with some convert scripts. Memory is free. But CPU usage become full.
Can we control the CPU usage?

Thanks
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Is it possible to control cpu usage?

Post by magick »

CPU should be 100% otherwise its idle and not working. However, to reduce CPU usage, set the throttle resource policy in policy.xml or set the MAGICK_THROTTLE environment variable. See http://www.imagemagick.org/script/resou ... nvironment.
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: Is it possible to control cpu usage?

Post by agriz »

i read that link. But I am not sure how to change the environment variable.
Can you guide me how to do it?

I am using centos.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Is it possible to control cpu usage?

Post by anthony »

Environment variables are just set of sting variables that are passed from parent programs to children

To set it for just one command put it before the command...

Code: Select all

MAGICK_THROTTLE=50  convert ...
The number is in milli-seconds and it means go to sleep this long at selected (convenient) points in the programs image processing to allow other programs to run.

However 100% usage should not be a problem. Other programs at an equal 'nice' level should also run.

Usually the worse problem is the use of multiple threads on a mutli-core processor. You can turn of multi-threading using...

Code: Select all

MAGICK_THREAD_LIMIT=1 convert ....
or more permanently set the variable using

Code: Select all

  export MAGICK_THREAD_LIMIT=1
You can list environment variables using the command env


For more on thread problems see IM Examples, Making IM Faster (in general)
http://www.imagemagick.org/Usage/api/#speed
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: Is it possible to control cpu usage?

Post by agriz »

/var/log/message is giving me the following warning

Code: Select all

kernel: possible SYN flooding on port 80. Sending cookies.
I hired a freelance to fix this problem.
But he is telling that convert command is using higher cpu usage.

The website is hardly running. It took very long time to load.

Top command gives something like this.

Code: Select all

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
10939 apache    20   0  204m 5824 2452 R 201.3  0.1   0:06.06 convert
10947 apache    20   0  135m 3828 2016 R 51.5  0.0   0:01.55 convert
10955 apache    20   0  198m 6064 2020 R 43.5  0.1   0:01.31 convert
10972 apache    20   0  198m 3732 2016 R  4.6  0.0   0:00.14 convert
I have already added

Code: Select all

export MAGICK_THREAD_LIMIT=1
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Is it possible to control cpu usage?

Post by magick »

To reduce the load ImageMagick puts on a server you can single thread it by building without OpenMP support or as you did, or limit the number of threads to 1 or 2. You can do this with the policy.xml configuration file, for example,
  • <policy domain="resource" name="thread" value="2"/>
    <policy domain="resource" name="throttle" value="1"/>
or use environment variables. In addition you can limit memory so that if a large image is processed its cached to disk rather than memory.

There is typically more overhead when using ImageMagick from the command-line. Instead use it from a scripting language, Perl or PHP, for example.

We have an ImageMagick web site @ http://www.imagemagick.org/MagickStudio/ that has supported thousands of image manipulations each day and the host computer is never overwhelmed. You can download the source @ http://www.imagemagick.org/download/per ... 9.3.tar.gz and see the sorts of things we do to ensure our host server runs efficiently.
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: Is it possible to control cpu usage?

Post by agriz »

I have downloaded and going to check it out.
Memory is not much used.

Only CPU is used a lot.
But i am quite confused whether it is an attack or overload which slows down the site.

Where is policy.xml file located?
/usr/bin/convert is the convert path on my computer
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: Is it possible to control cpu usage?

Post by agriz »

Also, I am using exec to process images with imagemagick.

How do i use PHP module? Do i need to install anything?
Will everything work in the same way as it is working in exec?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Is it possible to control cpu usage?

Post by fmw42 »

I believe that exec is a PHP command, so you are likely already using PHP, but it essentially is just an interface to the command line. But Magick was suggesting that you use something like Perlmagick or MagickWand for PHP or Imagick. See the various IM APIs at http://www.imagemagick.org/script/api.php
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: Is it possible to control cpu usage?

Post by agriz »

Using exec and imagic differs in performance?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Is it possible to control cpu usage?

Post by fmw42 »

User Bonzo and I did some limited timing tests for command line, PHP exec and Imagick and did not find that much difference. However, I suspect that Perlmagick may be different and faster.

Our timing tests are at:
viewtopic.php?f=4&t=16779
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: Is it possible to control cpu usage?

Post by agriz »

You have done nice work!!!

Code: Select all

convert -font Ravie -pointsize 72  label:'Get Wet!' -border 10 \
          -tile tile_aqua.jpg   -draw "color 0,0 reset"  \
          -tile tile_water.jpg -gravity center -annotate +0+0 'Get Wet!' \
          autosize_wet.jpg
from this page -- http://www.imagemagick.org/Usage/text/

Will it use more cpu memory than the following?

Code: Select all

convert -size 800x120 xc:black -font Corsiva -pointsize 100 \
          -tile tile_disks.jpg   -annotate +20+80 'Psychedelic!' \
          -trim +repage  -bordercolor black  -border 10   funfont_groovy.jpg
I just am asking because the first one is not using any size.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Is it possible to control cpu usage?

Post by fmw42 »

label: creates its own size image to match the pointsize and have no extra space around it. see http://www.imagemagick.org/Usage/text/#label
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Is it possible to control cpu usage?

Post by anthony »

The first uses label to create a canvas of about the right size. This is then cleared (tiled) and the font again re-drawn using -annotate so that we can use a tiling fill.

The other method is to just create a label using black and white only. Then use that as a composite mask operator to select different background foreground tile patterns. See... Coloring a Gray-scale Text Image
http://www.imagemagick.org/Usage/text/#coloring_text
and the sections this links to.
There are lots of ways to skin a cat, and what method you use depends
on what you want that skin for, and how messy you like the results!
-- Anthony Thyssen
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply