JNIEXPORT jobject JNICALL Java_org_grammaticalframework_sg_SG_openSG(JNIEnv *env, jclass cls, jstring path) { GuPool* tmp_pool = gu_local_pool(); // Create an exception frame that catches all errors. GuExn* err = gu_exn(tmp_pool); const char *fpath = (*env)->GetStringUTFChars(env, path, 0); // Read the PGF grammar. SgSG* sg = sg_open(fpath, err); (*env)->ReleaseStringUTFChars(env, path, fpath); if (!gu_ok(err)) { GuString msg; if (gu_exn_caught(err, SgError)) { msg = (GuString) gu_exn_caught_data(err); } else { msg = "The database cannot be opened"; } throw_string_exception(env, "org/grammaticalframework/sg/SGError", msg); gu_pool_free(tmp_pool); return NULL; } gu_pool_free(tmp_pool); jmethodID constrId = (*env)->GetMethodID(env, cls, "<init>", "(J)V"); return (*env)->NewObject(env, cls, constrId, p2l(sg)); }
static void scrub_disk(const char *path) { sg_t sg; int dev_type; scsi_vendor_t vendor; scsi_model_t model; scsi_fw_revision_t revision; scsi_serial_t serial; uint64_t num_blocks; uint32_t block_size; printf("Scrubbing disk %s\n", path); if (!sg_open(&sg, path)) { fprintf(stderr, "Error opening disk %s: %m\n", path); return; } if (!do_inquiry(&sg, vendor, model, revision, serial, &dev_type)) { fprintf(stderr, "Error while reading inquiry\n"); goto Exit; } if (dev_type != TYPE_DISK) { fprintf(stderr, "Device is not a disk, scrubbing makes no sense\n"); goto Exit; } if (!do_read_capacity(&sg, &num_blocks, &block_size)) { fprintf(stderr, "Error while reading capacity\n"); goto Exit; } printf("Device attributes:\n" "\tVendor: %s\n" "\tModel: %s\n" "\tRevision: %s\n" "\tSerial: %s\n" "\tNum blocks: %llu\n" "\tBlock size: %u\n" "\tSize: %llu GB\n" , vendor, model, revision, serial, (unsigned long long)num_blocks, block_size, (unsigned long long)num_blocks*block_size/1000/1000/1000); do_scrub(&sg, num_blocks, block_size); Exit: sg_close(&sg); }
static void survey_disk(const char *path) { sg_t sg; FILE *out = NULL; printf("Scrubbing disk %s\n", path); if (!sg_open(&sg, path)) { fprintf(stderr, "Error opening disk %s: %m\n", path); return; } int dev_type; scsi_vendor_t vendor; scsi_model_t model; scsi_fw_revision_t revision; scsi_serial_t serial; if (!do_inquiry(&sg, vendor, model, revision, serial, &dev_type)) { fprintf(stderr, "Error while reading inquiry\n"); goto Exit; } if (dev_type != TYPE_DISK) { fprintf(stderr, "Device is not a disk (%d), bailing out.\n", dev_type); goto Exit; } char filename[256]; snprintf(filename, sizeof(filename), "disk-survey-%s-%s-%s.xml", strtrim(vendor), strtrim(model), strtrim(serial)); out = fopen(filename, "w"); if (!out) { fprintf(stderr, "Failed to open log file '%s' for output\n", filename); goto Exit; } char largebuf[1024*1024]; setbuffer(out, largebuf, sizeof(largebuf)); fprintf(out, "<survey>\n" "\t<inquiry>\n" "\t\t<vendor>%s</vendor>\n" "\t\t<model>%s</model>\n" "\t\t<revision>%s</revision>\n" "\t\t<serial>%s</serial>\n" "\t\t<raw>%s</raw>\n" "\t</inquiry>\n" , vendor, model, revision, serial, hex_encode(inquiry_buf, sizeof(inquiry_buf), 1)); uint64_t num_blocks; uint32_t block_size; survey_disk_capacity(&sg, out, &num_blocks, &block_size); survey_vpds(&sg, out); survey_timestamp(&sg, out); survey_mode_pages(&sg, out); survey_read_diagnostics(&sg, out); if (disk_is_ata(vendor)) { survey_ata_identify(&sg, out); } // It woud be preferable to have no other IO during the read // performance test, so flush the pending output buffer to keep all of // the space for the next test fflush(out); survey_read_performance(&sg, out, num_blocks, block_size, path); Exit: if (out) { fprintf(out, "</survey>\n"); fclose(out); } sg_close(&sg); }