jni_helper.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "config.h"
  19. #include "exception.h"
  20. #include "jni_helper.h"
  21. #include <stdio.h>
  22. #include <string.h>
  23. static pthread_mutex_t hdfsHashMutex = PTHREAD_MUTEX_INITIALIZER;
  24. static pthread_mutex_t jvmMutex = PTHREAD_MUTEX_INITIALIZER;
  25. static volatile int hashTableInited = 0;
  26. #define LOCK_HASH_TABLE() pthread_mutex_lock(&hdfsHashMutex)
  27. #define UNLOCK_HASH_TABLE() pthread_mutex_unlock(&hdfsHashMutex)
  28. /** The Native return types that methods could return */
  29. #define VOID 'V'
  30. #define JOBJECT 'L'
  31. #define JARRAYOBJECT '['
  32. #define JBOOLEAN 'Z'
  33. #define JBYTE 'B'
  34. #define JCHAR 'C'
  35. #define JSHORT 'S'
  36. #define JINT 'I'
  37. #define JLONG 'J'
  38. #define JFLOAT 'F'
  39. #define JDOUBLE 'D'
  40. /**
  41. * MAX_HASH_TABLE_ELEM: The maximum no. of entries in the hashtable.
  42. * It's set to 4096 to account for (classNames + No. of threads)
  43. */
  44. #define MAX_HASH_TABLE_ELEM 4096
  45. /** Key that allows us to retrieve thread-local storage */
  46. static pthread_key_t gTlsKey;
  47. /** nonzero if we succeeded in initializing gTlsKey. Protected by the jvmMutex */
  48. static int gTlsKeyInitialized = 0;
  49. /** Pthreads thread-local storage for each library thread. */
  50. struct hdfsTls {
  51. JNIEnv *env;
  52. };
  53. /**
  54. * The function that is called whenever a thread with libhdfs thread local data
  55. * is destroyed.
  56. *
  57. * @param v The thread-local data
  58. */
  59. static void hdfsThreadDestructor(void *v)
  60. {
  61. struct hdfsTls *tls = v;
  62. JavaVM *vm;
  63. JNIEnv *env = tls->env;
  64. jint ret;
  65. ret = (*env)->GetJavaVM(env, &vm);
  66. if (ret) {
  67. fprintf(stderr, "hdfsThreadDestructor: GetJavaVM failed with "
  68. "error %d\n", ret);
  69. (*env)->ExceptionDescribe(env);
  70. } else {
  71. (*vm)->DetachCurrentThread(vm);
  72. }
  73. free(tls);
  74. }
  75. void destroyLocalReference(JNIEnv *env, jobject jObject)
  76. {
  77. if (jObject)
  78. (*env)->DeleteLocalRef(env, jObject);
  79. }
  80. static jthrowable validateMethodType(JNIEnv *env, MethType methType)
  81. {
  82. if (methType != STATIC && methType != INSTANCE) {
  83. return newRuntimeError(env, "validateMethodType(methType=%d): "
  84. "illegal method type.\n", methType);
  85. }
  86. return NULL;
  87. }
  88. jthrowable newJavaStr(JNIEnv *env, const char *str, jstring *out)
  89. {
  90. jstring jstr;
  91. if (!str) {
  92. /* Can't pass NULL to NewStringUTF: the result would be
  93. * implementation-defined. */
  94. *out = NULL;
  95. return NULL;
  96. }
  97. jstr = (*env)->NewStringUTF(env, str);
  98. if (!jstr) {
  99. /* If NewStringUTF returns NULL, an exception has been thrown,
  100. * which we need to handle. Probaly an OOM. */
  101. return getPendingExceptionAndClear(env);
  102. }
  103. *out = jstr;
  104. return NULL;
  105. }
  106. jthrowable newCStr(JNIEnv *env, jstring jstr, char **out)
  107. {
  108. const char *tmp;
  109. if (!jstr) {
  110. *out = NULL;
  111. return NULL;
  112. }
  113. tmp = (*env)->GetStringUTFChars(env, jstr, NULL);
  114. if (!tmp) {
  115. return getPendingExceptionAndClear(env);
  116. }
  117. *out = strdup(tmp);
  118. (*env)->ReleaseStringUTFChars(env, jstr, tmp);
  119. return NULL;
  120. }
  121. static int hashTableInit(void)
  122. {
  123. if (!hashTableInited) {
  124. LOCK_HASH_TABLE();
  125. if (!hashTableInited) {
  126. if (hcreate(MAX_HASH_TABLE_ELEM) == 0) {
  127. fprintf(stderr, "error creating hashtable, <%d>: %s\n",
  128. errno, strerror(errno));
  129. UNLOCK_HASH_TABLE();
  130. return 0;
  131. }
  132. hashTableInited = 1;
  133. }
  134. UNLOCK_HASH_TABLE();
  135. }
  136. return 1;
  137. }
  138. static int insertEntryIntoTable(const char *key, void *data)
  139. {
  140. ENTRY e, *ep;
  141. if (key == NULL || data == NULL) {
  142. return 0;
  143. }
  144. if (! hashTableInit()) {
  145. return -1;
  146. }
  147. e.data = data;
  148. e.key = (char*)key;
  149. LOCK_HASH_TABLE();
  150. ep = hsearch(e, ENTER);
  151. UNLOCK_HASH_TABLE();
  152. if (ep == NULL) {
  153. fprintf(stderr, "warn adding key (%s) to hash table, <%d>: %s\n",
  154. key, errno, strerror(errno));
  155. }
  156. return 0;
  157. }
  158. static void* searchEntryFromTable(const char *key)
  159. {
  160. ENTRY e,*ep;
  161. if (key == NULL) {
  162. return NULL;
  163. }
  164. hashTableInit();
  165. e.key = (char*)key;
  166. LOCK_HASH_TABLE();
  167. ep = hsearch(e, FIND);
  168. UNLOCK_HASH_TABLE();
  169. if (ep != NULL) {
  170. return ep->data;
  171. }
  172. return NULL;
  173. }
  174. jthrowable invokeMethod(JNIEnv *env, jvalue *retval, MethType methType,
  175. jobject instObj, const char *className,
  176. const char *methName, const char *methSignature, ...)
  177. {
  178. va_list args;
  179. jclass cls;
  180. jmethodID mid;
  181. jthrowable jthr;
  182. const char *str;
  183. char returnType;
  184. jthr = validateMethodType(env, methType);
  185. if (jthr)
  186. return jthr;
  187. jthr = globalClassReference(className, env, &cls);
  188. if (jthr)
  189. return jthr;
  190. jthr = methodIdFromClass(className, methName, methSignature,
  191. methType, env, &mid);
  192. if (jthr)
  193. return jthr;
  194. str = methSignature;
  195. while (*str != ')') str++;
  196. str++;
  197. returnType = *str;
  198. va_start(args, methSignature);
  199. if (returnType == JOBJECT || returnType == JARRAYOBJECT) {
  200. jobject jobj = NULL;
  201. if (methType == STATIC) {
  202. jobj = (*env)->CallStaticObjectMethodV(env, cls, mid, args);
  203. }
  204. else if (methType == INSTANCE) {
  205. jobj = (*env)->CallObjectMethodV(env, instObj, mid, args);
  206. }
  207. retval->l = jobj;
  208. }
  209. else if (returnType == VOID) {
  210. if (methType == STATIC) {
  211. (*env)->CallStaticVoidMethodV(env, cls, mid, args);
  212. }
  213. else if (methType == INSTANCE) {
  214. (*env)->CallVoidMethodV(env, instObj, mid, args);
  215. }
  216. }
  217. else if (returnType == JBOOLEAN) {
  218. jboolean jbool = 0;
  219. if (methType == STATIC) {
  220. jbool = (*env)->CallStaticBooleanMethodV(env, cls, mid, args);
  221. }
  222. else if (methType == INSTANCE) {
  223. jbool = (*env)->CallBooleanMethodV(env, instObj, mid, args);
  224. }
  225. retval->z = jbool;
  226. }
  227. else if (returnType == JSHORT) {
  228. jshort js = 0;
  229. if (methType == STATIC) {
  230. js = (*env)->CallStaticShortMethodV(env, cls, mid, args);
  231. }
  232. else if (methType == INSTANCE) {
  233. js = (*env)->CallShortMethodV(env, instObj, mid, args);
  234. }
  235. retval->s = js;
  236. }
  237. else if (returnType == JLONG) {
  238. jlong jl = -1;
  239. if (methType == STATIC) {
  240. jl = (*env)->CallStaticLongMethodV(env, cls, mid, args);
  241. }
  242. else if (methType == INSTANCE) {
  243. jl = (*env)->CallLongMethodV(env, instObj, mid, args);
  244. }
  245. retval->j = jl;
  246. }
  247. else if (returnType == JINT) {
  248. jint ji = -1;
  249. if (methType == STATIC) {
  250. ji = (*env)->CallStaticIntMethodV(env, cls, mid, args);
  251. }
  252. else if (methType == INSTANCE) {
  253. ji = (*env)->CallIntMethodV(env, instObj, mid, args);
  254. }
  255. retval->i = ji;
  256. }
  257. va_end(args);
  258. jthr = (*env)->ExceptionOccurred(env);
  259. if (jthr) {
  260. (*env)->ExceptionClear(env);
  261. return jthr;
  262. }
  263. return NULL;
  264. }
  265. jthrowable constructNewObjectOfClass(JNIEnv *env, jobject *out, const char *className,
  266. const char *ctorSignature, ...)
  267. {
  268. va_list args;
  269. jclass cls;
  270. jmethodID mid;
  271. jobject jobj;
  272. jthrowable jthr;
  273. jthr = globalClassReference(className, env, &cls);
  274. if (jthr)
  275. return jthr;
  276. jthr = methodIdFromClass(className, "<init>", ctorSignature,
  277. INSTANCE, env, &mid);
  278. if (jthr)
  279. return jthr;
  280. va_start(args, ctorSignature);
  281. jobj = (*env)->NewObjectV(env, cls, mid, args);
  282. va_end(args);
  283. if (!jobj)
  284. return getPendingExceptionAndClear(env);
  285. *out = jobj;
  286. return NULL;
  287. }
  288. jthrowable methodIdFromClass(const char *className, const char *methName,
  289. const char *methSignature, MethType methType,
  290. JNIEnv *env, jmethodID *out)
  291. {
  292. jclass cls;
  293. jthrowable jthr;
  294. jthr = globalClassReference(className, env, &cls);
  295. if (jthr)
  296. return jthr;
  297. jmethodID mid = 0;
  298. jthr = validateMethodType(env, methType);
  299. if (jthr)
  300. return jthr;
  301. if (methType == STATIC) {
  302. mid = (*env)->GetStaticMethodID(env, cls, methName, methSignature);
  303. }
  304. else if (methType == INSTANCE) {
  305. mid = (*env)->GetMethodID(env, cls, methName, methSignature);
  306. }
  307. if (mid == NULL) {
  308. fprintf(stderr, "could not find method %s from class %s with "
  309. "signature %s\n", methName, className, methSignature);
  310. return getPendingExceptionAndClear(env);
  311. }
  312. *out = mid;
  313. return NULL;
  314. }
  315. jthrowable globalClassReference(const char *className, JNIEnv *env, jclass *out)
  316. {
  317. jclass clsLocalRef;
  318. jclass cls = searchEntryFromTable(className);
  319. if (cls) {
  320. *out = cls;
  321. return NULL;
  322. }
  323. clsLocalRef = (*env)->FindClass(env,className);
  324. if (clsLocalRef == NULL) {
  325. return getPendingExceptionAndClear(env);
  326. }
  327. cls = (*env)->NewGlobalRef(env, clsLocalRef);
  328. if (cls == NULL) {
  329. (*env)->DeleteLocalRef(env, clsLocalRef);
  330. return getPendingExceptionAndClear(env);
  331. }
  332. (*env)->DeleteLocalRef(env, clsLocalRef);
  333. insertEntryIntoTable(className, cls);
  334. *out = cls;
  335. return NULL;
  336. }
  337. jthrowable classNameOfObject(jobject jobj, JNIEnv *env, char **name)
  338. {
  339. jthrowable jthr;
  340. jclass cls, clsClass = NULL;
  341. jmethodID mid;
  342. jstring str = NULL;
  343. const char *cstr = NULL;
  344. char *newstr;
  345. cls = (*env)->GetObjectClass(env, jobj);
  346. if (cls == NULL) {
  347. jthr = getPendingExceptionAndClear(env);
  348. goto done;
  349. }
  350. clsClass = (*env)->FindClass(env, "java/lang/Class");
  351. if (clsClass == NULL) {
  352. jthr = getPendingExceptionAndClear(env);
  353. goto done;
  354. }
  355. mid = (*env)->GetMethodID(env, clsClass, "getName", "()Ljava/lang/String;");
  356. if (mid == NULL) {
  357. jthr = getPendingExceptionAndClear(env);
  358. goto done;
  359. }
  360. str = (*env)->CallObjectMethod(env, cls, mid);
  361. if (str == NULL) {
  362. jthr = getPendingExceptionAndClear(env);
  363. goto done;
  364. }
  365. cstr = (*env)->GetStringUTFChars(env, str, NULL);
  366. if (!cstr) {
  367. jthr = getPendingExceptionAndClear(env);
  368. goto done;
  369. }
  370. newstr = strdup(cstr);
  371. if (newstr == NULL) {
  372. jthr = newRuntimeError(env, "classNameOfObject: out of memory");
  373. goto done;
  374. }
  375. *name = newstr;
  376. jthr = NULL;
  377. done:
  378. destroyLocalReference(env, cls);
  379. destroyLocalReference(env, clsClass);
  380. if (str) {
  381. if (cstr)
  382. (*env)->ReleaseStringUTFChars(env, str, cstr);
  383. (*env)->DeleteLocalRef(env, str);
  384. }
  385. return jthr;
  386. }
  387. /**
  388. * Get the global JNI environemnt.
  389. *
  390. * We only have to create the JVM once. After that, we can use it in
  391. * every thread. You must be holding the jvmMutex when you call this
  392. * function.
  393. *
  394. * @return The JNIEnv on success; error code otherwise
  395. */
  396. static JNIEnv* getGlobalJNIEnv(void)
  397. {
  398. const jsize vmBufLength = 1;
  399. JavaVM* vmBuf[vmBufLength];
  400. JNIEnv *env;
  401. jint rv = 0;
  402. jint noVMs = 0;
  403. jthrowable jthr;
  404. rv = JNI_GetCreatedJavaVMs(&(vmBuf[0]), vmBufLength, &noVMs);
  405. if (rv != 0) {
  406. fprintf(stderr, "JNI_GetCreatedJavaVMs failed with error: %d\n", rv);
  407. return NULL;
  408. }
  409. if (noVMs == 0) {
  410. //Get the environment variables for initializing the JVM
  411. char *hadoopClassPath = getenv("CLASSPATH");
  412. if (hadoopClassPath == NULL) {
  413. fprintf(stderr, "Environment variable CLASSPATH not set!\n");
  414. return NULL;
  415. }
  416. char *hadoopClassPathVMArg = "-Djava.class.path=";
  417. size_t optHadoopClassPathLen = strlen(hadoopClassPath) +
  418. strlen(hadoopClassPathVMArg) + 1;
  419. char *optHadoopClassPath = malloc(sizeof(char)*optHadoopClassPathLen);
  420. snprintf(optHadoopClassPath, optHadoopClassPathLen,
  421. "%s%s", hadoopClassPathVMArg, hadoopClassPath);
  422. // Determine the # of LIBHDFS_OPTS args
  423. int noArgs = 1;
  424. char *hadoopJvmArgs = getenv("LIBHDFS_OPTS");
  425. char jvmArgDelims[] = " ";
  426. char *str, *token, *savePtr;
  427. if (hadoopJvmArgs != NULL) {
  428. hadoopJvmArgs = strdup(hadoopJvmArgs);
  429. for (noArgs = 1, str = hadoopJvmArgs; ; noArgs++, str = NULL) {
  430. token = strtok_r(str, jvmArgDelims, &savePtr);
  431. if (NULL == token) {
  432. break;
  433. }
  434. }
  435. free(hadoopJvmArgs);
  436. }
  437. // Now that we know the # args, populate the options array
  438. JavaVMOption options[noArgs];
  439. options[0].optionString = optHadoopClassPath;
  440. hadoopJvmArgs = getenv("LIBHDFS_OPTS");
  441. if (hadoopJvmArgs != NULL) {
  442. hadoopJvmArgs = strdup(hadoopJvmArgs);
  443. for (noArgs = 1, str = hadoopJvmArgs; ; noArgs++, str = NULL) {
  444. token = strtok_r(str, jvmArgDelims, &savePtr);
  445. if (NULL == token) {
  446. break;
  447. }
  448. options[noArgs].optionString = token;
  449. }
  450. }
  451. //Create the VM
  452. JavaVMInitArgs vm_args;
  453. JavaVM *vm;
  454. vm_args.version = JNI_VERSION_1_2;
  455. vm_args.options = options;
  456. vm_args.nOptions = noArgs;
  457. vm_args.ignoreUnrecognized = 1;
  458. rv = JNI_CreateJavaVM(&vm, (void*)&env, &vm_args);
  459. if (hadoopJvmArgs != NULL) {
  460. free(hadoopJvmArgs);
  461. }
  462. free(optHadoopClassPath);
  463. if (rv != 0) {
  464. fprintf(stderr, "Call to JNI_CreateJavaVM failed "
  465. "with error: %d\n", rv);
  466. return NULL;
  467. }
  468. jthr = invokeMethod(env, NULL, STATIC, NULL,
  469. "org/apache/hadoop/fs/FileSystem",
  470. "loadFileSystems", "()V");
  471. if (jthr) {
  472. printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "loadFileSystems");
  473. }
  474. }
  475. else {
  476. //Attach this thread to the VM
  477. JavaVM* vm = vmBuf[0];
  478. rv = (*vm)->AttachCurrentThread(vm, (void*)&env, 0);
  479. if (rv != 0) {
  480. fprintf(stderr, "Call to AttachCurrentThread "
  481. "failed with error: %d\n", rv);
  482. return NULL;
  483. }
  484. }
  485. return env;
  486. }
  487. /**
  488. * getJNIEnv: A helper function to get the JNIEnv* for the given thread.
  489. * If no JVM exists, then one will be created. JVM command line arguments
  490. * are obtained from the LIBHDFS_OPTS environment variable.
  491. *
  492. * Implementation note: we rely on POSIX thread-local storage (tls).
  493. * This allows us to associate a destructor function with each thread, that
  494. * will detach the thread from the Java VM when the thread terminates. If we
  495. * failt to do this, it will cause a memory leak.
  496. *
  497. * However, POSIX TLS is not the most efficient way to do things. It requires a
  498. * key to be initialized before it can be used. Since we don't know if this key
  499. * is initialized at the start of this function, we have to lock a mutex first
  500. * and check. Luckily, most operating systems support the more efficient
  501. * __thread construct, which is initialized by the linker.
  502. *
  503. * @param: None.
  504. * @return The JNIEnv* corresponding to the thread.
  505. */
  506. JNIEnv* getJNIEnv(void)
  507. {
  508. JNIEnv *env;
  509. struct hdfsTls *tls;
  510. int ret;
  511. #ifdef HAVE_BETTER_TLS
  512. static __thread struct hdfsTls *quickTls = NULL;
  513. if (quickTls)
  514. return quickTls->env;
  515. #endif
  516. pthread_mutex_lock(&jvmMutex);
  517. if (!gTlsKeyInitialized) {
  518. ret = pthread_key_create(&gTlsKey, hdfsThreadDestructor);
  519. if (ret) {
  520. pthread_mutex_unlock(&jvmMutex);
  521. fprintf(stderr, "getJNIEnv: pthread_key_create failed with "
  522. "error %d\n", ret);
  523. return NULL;
  524. }
  525. gTlsKeyInitialized = 1;
  526. }
  527. tls = pthread_getspecific(gTlsKey);
  528. if (tls) {
  529. pthread_mutex_unlock(&jvmMutex);
  530. return tls->env;
  531. }
  532. env = getGlobalJNIEnv();
  533. pthread_mutex_unlock(&jvmMutex);
  534. if (!env) {
  535. fprintf(stderr, "getJNIEnv: getGlobalJNIEnv failed\n");
  536. return NULL;
  537. }
  538. tls = calloc(1, sizeof(struct hdfsTls));
  539. if (!tls) {
  540. fprintf(stderr, "getJNIEnv: OOM allocating %zd bytes\n",
  541. sizeof(struct hdfsTls));
  542. return NULL;
  543. }
  544. tls->env = env;
  545. ret = pthread_setspecific(gTlsKey, tls);
  546. if (ret) {
  547. fprintf(stderr, "getJNIEnv: pthread_setspecific failed with "
  548. "error code %d\n", ret);
  549. hdfsThreadDestructor(tls);
  550. return NULL;
  551. }
  552. #ifdef HAVE_BETTER_TLS
  553. quickTls = tls;
  554. #endif
  555. return env;
  556. }