Single PMS running

Discuss issues related to PS3 Media Server development (only for programmers)

Single PMS running

Postby SharkHunter » Tue Jun 21, 2011 7:55 am

Hi all,

Finally got tired of all those time when PMS wasn't killed properly and left some old "javaw" process laying around. The first thing that came into my mind was to do the classic UNIX trick of saving the pid and the when you start a new PMS it slays that process. Windows is not that keen on supplying a equivalent to the $$ so it had to be a more advanced SharkHunter hack. The principal is the same stash the pid in a file kill the process when you start again. It is done completely from within the PMS java for the reason that I wanted this to work even if you have some custom start script.

I'll make a proper patch later this is just the code....(of course you need to call killOld() as well)

Code: Select all
private static void killOld() throws IOException {
      try {
         killProc();
      } catch (IOException e) {
      }
      dumpPid();
   }
   
   private static void killProc() throws IOException {
      ProcessBuilder pb=null;
      BufferedReader in = new BufferedReader(new FileReader("pms.pid"));
      String pid=in.readLine();
      in.close();
      if(Platform.isWindows()) {
         pb=new ProcessBuilder("taskkill","/F","/PID",pid,"/T");
      }
      else if(Platform.isFreeBSD()||Platform.isLinux()||Platform.isOpenBSD()||Platform.isSolaris())
         pb=new ProcessBuilder("kill","-9",pid);
      if(pb==null)
         return;
      try {         
         Process p=pb.start();
         p.waitFor();
      } catch (Exception e) {
         debug("error kill pid "+e);
      }
   }
   
   public static long getPID() {
       String processName =
         java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
       return Long.parseLong(processName.split("@")[0]);
     }
   
   private static void dumpPid() throws IOException {
      FileOutputStream out=new FileOutputStream("pms.pid");
      String data=String.valueOf(getPID())+"\r\n";
      out.write(data.getBytes());
      out.flush();
      out.close();
   }
SharkHunter
 
Posts: 941
Joined: Tue Jun 01, 2010 8:39 pm

Re: Single PMS running

Postby Raptor399 » Tue Jun 21, 2011 8:56 am

Wouldn't the same thing also work on OSX?
In which case it could simply be:

Code: Select all
 else {
         pb=new ProcessBuilder("kill","-9",pid);
}


I never encountered a problem with a resident PMS on OSX, but checking can't hurt. ;-)

P.S. Off-topic: I did encounter a hanging mencoder process on multiple occasions though. Just never found a way to reproduce the bug.
Raptor399
Project Member
 
Posts: 1916
Joined: Thu Mar 10, 2011 12:06 am

Re: Single PMS running

Postby SharkHunter » Tue Jun 21, 2011 9:05 am

I don't know OSX but I suspected "someone" (=you :)) would spot that and add the OSX specific kill call.

Edit : hanging mencoders is something I found as well from time to time. This could be extended to stash the pid of all processes started by PMS and then slay them on startup...
SharkHunter
 
Posts: 941
Joined: Tue Jun 01, 2010 8:39 pm

Re: Single PMS running

Postby Redlum » Tue Jun 21, 2011 10:50 am

Kill -9 is quite dirty though, we shouldn't really use it.

Also see https://code.google.com/p/ps3mediaserve ... ail?id=680
Redlum
Project Member
 
Posts: 371
Joined: Mon Jan 12, 2009 9:13 pm

Re: Single PMS running

Postby Raptor399 » Wed Jun 22, 2011 8:21 am

I thought I'd hook up PMS to VisualVM via Eclipse, just to see what is going on in there.

It just so happens that I got two OutOfMemory errors in "net.pms.io.OutputBufferConsumer" (line 51) which resulted in two hanging mencoder processes.

Of course it wasn't a normal setting, running PMS from Eclipse while being monitored by VisualVM. And a crash while viewing the testmovie was also not normal, so there might be something else going on. But still... Maybe worth some more investigation. :-)
Raptor399
Project Member
 
Posts: 1916
Joined: Thu Mar 10, 2011 12:06 am

Re: Single PMS running

Postby SharkHunter » Wed Jun 22, 2011 8:37 pm

I get a lot of mencoder process hanging if I don't let them terminate by them self. Maybe we should do the classic UNIX solution to dump all pids to a file and the just do kill -9 'cat file'? Hanging process are a pain...
SharkHunter
 
Posts: 941
Joined: Tue Jun 01, 2010 8:39 pm

Re: Single PMS running

Postby Redlum » Sun Jun 26, 2011 1:35 pm

It's a better workaround then we have now and yes, you can use the same calls on OS X. It would be better to include all processes spawned by PMS as hanging mencoder processes are more common afaik. If you can make a patch we would definitely take it I think.

Obviously it would be best to fix the underlying problem, what Raptor339 said is an interesting start.
Redlum
Project Member
 
Posts: 371
Joined: Mon Jan 12, 2009 9:13 pm

Re: Single PMS running

Postby chocolateboy » Sun Jun 26, 2011 2:02 pm

SharkHunter wrote:This could be extended to stash the pid of all processes started by PMS and then slay them on startup...


This will result in unrelated processes being killed:

viewtopic.php?f=14&t=7844&p=38799&p=38719#p38719
viewtopic.php?f=14&t=7844&p=38799&p=38729#p38729
viewtopic.php?f=14&t=7844&p=38799&p=38799#p38799
chocolateboy
Project Member
 
Posts: 2577
Joined: Wed Sep 16, 2009 10:05 am


Return to Developers

Who is online

Users browsing this forum: No registered users and 4 guests