hotspot/src/os/linux/vm/os_linux.cpp
changeset 27474 2b061fd571eb
parent 27471 6e56277909f1
parent 27473 07948a5f8f5c
child 27880 afb974a04396
child 27685 26a697375de3
child 27562 47f369e3c69c
child 29180 50369728b00e
equal deleted inserted replaced
27472:fc8b041bd744 27474:2b061fd571eb
   160 // Used to protect dlsym() calls
   160 // Used to protect dlsym() calls
   161 static pthread_mutex_t dl_mutex;
   161 static pthread_mutex_t dl_mutex;
   162 
   162 
   163 // Declarations
   163 // Declarations
   164 static void unpackTime(timespec* absTime, bool isAbsolute, jlong time);
   164 static void unpackTime(timespec* absTime, bool isAbsolute, jlong time);
   165 
       
   166 #ifdef JAVASE_EMBEDDED
       
   167 class MemNotifyThread: public Thread {
       
   168   friend class VMStructs;
       
   169  public:
       
   170   virtual void run();
       
   171 
       
   172  private:
       
   173   static MemNotifyThread* _memnotify_thread;
       
   174   int _fd;
       
   175 
       
   176  public:
       
   177 
       
   178   // Constructor
       
   179   MemNotifyThread(int fd);
       
   180 
       
   181   // Tester
       
   182   bool is_memnotify_thread() const { return true; }
       
   183 
       
   184   // Printing
       
   185   char* name() const { return (char*)"Linux MemNotify Thread"; }
       
   186 
       
   187   // Returns the single instance of the MemNotifyThread
       
   188   static MemNotifyThread* memnotify_thread() { return _memnotify_thread; }
       
   189 
       
   190   // Create and start the single instance of MemNotifyThread
       
   191   static void start();
       
   192 };
       
   193 #endif // JAVASE_EMBEDDED
       
   194 
   165 
   195 // utility functions
   166 // utility functions
   196 
   167 
   197 static int SR_initialize();
   168 static int SR_initialize();
   198 
   169 
  4869 
  4840 
  4870   // initialize thread priority policy
  4841   // initialize thread priority policy
  4871   prio_init();
  4842   prio_init();
  4872 
  4843 
  4873   return JNI_OK;
  4844   return JNI_OK;
  4874 }
       
  4875 
       
  4876 // this is called at the end of vm_initialization
       
  4877 void os::init_3(void) {
       
  4878 #ifdef JAVASE_EMBEDDED
       
  4879   // Start the MemNotifyThread
       
  4880   if (LowMemoryProtection) {
       
  4881     MemNotifyThread::start();
       
  4882   }
       
  4883   return;
       
  4884 #endif
       
  4885 }
  4845 }
  4886 
  4846 
  4887 // Mark the polling page as unreadable
  4847 // Mark the polling page as unreadable
  4888 void os::make_polling_page_unreadable(void) {
  4848 void os::make_polling_page_unreadable(void) {
  4889   if (!guard_memory((char*)_polling_page, Linux::page_size())) {
  4849   if (!guard_memory((char*)_polling_page, Linux::page_size())) {
  6038   }
  5998   }
  6039 
  5999 
  6040   return strlen(buffer);
  6000   return strlen(buffer);
  6041 }
  6001 }
  6042 
  6002 
  6043 #ifdef JAVASE_EMBEDDED
       
  6044 //
       
  6045 // A thread to watch the '/dev/mem_notify' device, which will tell us when the OS is running low on memory.
       
  6046 //
       
  6047 MemNotifyThread* MemNotifyThread::_memnotify_thread = NULL;
       
  6048 
       
  6049 // ctor
       
  6050 //
       
  6051 MemNotifyThread::MemNotifyThread(int fd): Thread() {
       
  6052   assert(memnotify_thread() == NULL, "we can only allocate one MemNotifyThread");
       
  6053   _fd = fd;
       
  6054 
       
  6055   if (os::create_thread(this, os::os_thread)) {
       
  6056     _memnotify_thread = this;
       
  6057     os::set_priority(this, NearMaxPriority);
       
  6058     os::start_thread(this);
       
  6059   }
       
  6060 }
       
  6061 
       
  6062 // Where all the work gets done
       
  6063 //
       
  6064 void MemNotifyThread::run() {
       
  6065   assert(this == memnotify_thread(), "expected the singleton MemNotifyThread");
       
  6066 
       
  6067   // Set up the select arguments
       
  6068   fd_set rfds;
       
  6069   if (_fd != -1) {
       
  6070     FD_ZERO(&rfds);
       
  6071     FD_SET(_fd, &rfds);
       
  6072   }
       
  6073 
       
  6074   // Now wait for the mem_notify device to wake up
       
  6075   while (1) {
       
  6076     // Wait for the mem_notify device to signal us..
       
  6077     int rc = select(_fd+1, _fd != -1 ? &rfds : NULL, NULL, NULL, NULL);
       
  6078     if (rc == -1) {
       
  6079       perror("select!\n");
       
  6080       break;
       
  6081     } else if (rc) {
       
  6082       //ssize_t free_before = os::available_memory();
       
  6083       //tty->print ("Notified: Free: %dK \n",os::available_memory()/1024);
       
  6084 
       
  6085       // The kernel is telling us there is not much memory left...
       
  6086       // try to do something about that
       
  6087 
       
  6088       // If we are not already in a GC, try one.
       
  6089       if (!Universe::heap()->is_gc_active()) {
       
  6090         Universe::heap()->collect(GCCause::_allocation_failure);
       
  6091 
       
  6092         //ssize_t free_after = os::available_memory();
       
  6093         //tty->print ("Post-Notify: Free: %dK\n",free_after/1024);
       
  6094         //tty->print ("GC freed: %dK\n", (free_after - free_before)/1024);
       
  6095       }
       
  6096       // We might want to do something like the following if we find the GC's are not helping...
       
  6097       // Universe::heap()->size_policy()->set_gc_time_limit_exceeded(true);
       
  6098     }
       
  6099   }
       
  6100 }
       
  6101 
       
  6102 // See if the /dev/mem_notify device exists, and if so, start a thread to monitor it.
       
  6103 //
       
  6104 void MemNotifyThread::start() {
       
  6105   int fd;
       
  6106   fd = open("/dev/mem_notify", O_RDONLY, 0);
       
  6107   if (fd < 0) {
       
  6108     return;
       
  6109   }
       
  6110 
       
  6111   if (memnotify_thread() == NULL) {
       
  6112     new MemNotifyThread(fd);
       
  6113   }
       
  6114 }
       
  6115 
       
  6116 #endif // JAVASE_EMBEDDED
       
  6117 
       
  6118 
       
  6119 /////////////// Unit tests ///////////////
  6003 /////////////// Unit tests ///////////////
  6120 
  6004 
  6121 #ifndef PRODUCT
  6005 #ifndef PRODUCT
  6122 
  6006 
  6123 #define test_log(...)              \
  6007 #define test_log(...)              \